Skip to content

Commit 4a5f2f9

Browse files
author
Thomas Darimont
committed
Initial import
1 parent 95d4f0a commit 4a5f2f9

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

ex010-ownership/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "ex010-ownership"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

ex010-ownership/src/main.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
fn main() {
2+
demo_ownership_beginns_with_assignment();
3+
4+
demo_ownership_beginns_with_assignment_ends_with_scope();
5+
6+
demo_reassignment_moves_ownership();
7+
}
8+
9+
fn demo_ownership_beginns_with_assignment() {
10+
let x = 42; // The assignee (x) becomes the value's sole owner
11+
12+
println!("x: {}", x);
13+
}
14+
15+
fn demo_ownership_beginns_with_assignment_ends_with_scope() {
16+
{
17+
let x = 21; // The assignee (x) becomes the value's sole owner
18+
19+
println!("x: {}", x);
20+
}
21+
22+
// println!("x: {}", x); // ERROR: x not in scope
23+
}
24+
25+
fn demo_reassignment_moves_ownership() {
26+
let a = vec![1, 2, 3];
27+
let b = a;
28+
29+
println!("b: {:?}", b);
30+
// println!("a: {:?}", a); // ERROR: borrow of moved value: `a`
31+
}

helloworld/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "helloworld"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

helloworld/src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

snippets.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cargo new helloworld
2+
3+
cargo run
4+
5+
cargo new ex010-ownership

0 commit comments

Comments
 (0)