File tree 5 files changed +55
-0
lines changed
5 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
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 ]
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 ]
Original file line number Diff line number Diff line change
1
+ fn main ( ) {
2
+ println ! ( "Hello, world!" ) ;
3
+ }
Original file line number Diff line number Diff line change
1
+ cargo new helloworld
2
+
3
+ cargo run
4
+
5
+ cargo new ex010-ownership
You can’t perform that action at this time.
0 commit comments