|
1 | 1 | - [What is Ownership? - The Rust Programming Language](https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html) |
2 | | - - [[Rust/Variable]]s by live in the scope of function's stack, and are deallocated at the end of the function. |
3 | | - - [[Rust/Box]]es, in contrast, live in the heap. |
| 2 | + - [[Rust/Variable]]s by live in the scope of function's [[Programming/Concept/Stack]], and are deallocated at the end of the function. |
| 3 | + - [[Rust/Box]]es, in contrast, live in the [[Programming/Concept/Heap]]. |
4 | 4 | - Internally, [[Rust/Collection]]s like [[Rust/Vec]] and [[Rust/String]] use `Box`es. |
5 | 5 | - Rust is unique because it does not permit manual memory management. |
6 | 6 | - [[Rust/Variable]]s cannot be used after being moved. |
|
14 | 14 | } |
15 | 15 | ``` |
16 | 16 | - To get around this, you can clone the string so there are two copies. |
17 | | - - [[Rust/Ownership]] comes into play in the difference between these two phrases: |
| 17 | + - [[Rust/Owner/ship]] comes into play in the difference between these two phrases: |
18 | 18 | - **Box deallocation principle (almost correct):** if a variable **is bound to** a `Box`, when rust deallocates the variable's frame, then Rust deallocates the box's heap memory. |
19 | 19 | - **Box deallocation principle (fully correct):** if a `Box` **is owned by** a variable, when Rust deallocates the variable's frame, then Rust deallocates the Box's heap's memory. |
20 | 20 | - Here ownership refer s to how variables can fall out of scope. |
21 | 21 | - None of the following example compile; |
22 | | - - ```rust |
23 | | - // example 1 |
24 | | - let b = Box::new(0); |
25 | | - let b2 = b; // this is okay |
26 | | - move_a_box(b); // not okay: Box is not owned by b |
27 | | - |
28 | | - // example 2 |
29 | | - let b = Box::new(0); |
30 | | - move_a_box(b); // Box passed to func. when done, rust deallocates its mem |
31 | | - println!("{}", b); // Box is not owned by b |
32 | | - |
33 | | - // example 3 |
34 | | - let b = Box::new(0); |
35 | | - move_a_box(b); // Box passed to func. when done, rust deallocates its mem |
36 | | - let b2 = b; // box is not owned by b |
37 | | - ``` |
| 22 | + - ```rust |
| 23 | + // example 1 |
| 24 | + let b = Box::new(0); |
| 25 | + let b2 = b; // this is okay |
| 26 | + move_a_box(b); // not okay: Box is not owned by b |
| 27 | + |
| 28 | + // example 2 |
| 29 | + let b = Box::new(0); |
| 30 | + move_a_box(b); // Box passed to func. when done, rust deallocates its mem |
| 31 | + println!("{}", b); // Box is not owned by b |
| 32 | + |
| 33 | + // example 3 |
| 34 | + let b = Box::new(0); |
| 35 | + move_a_box(b); // Box passed to func. when done, rust deallocates its mem |
| 36 | + let b2 = b; // box is not owned by b |
| 37 | + ``` |
| 38 | + - Here's an example of the compilation error for the first one: |
| 39 | + - code |
| 40 | + - ```rust |
| 41 | + |
| 42 | + fn move_a_box(b: Box<i32>) { |
| 43 | + println!("b in move_a_box: {b}"); |
| 44 | + } |
| 45 | + |
| 46 | + fn main() { |
| 47 | + let b = Box::new(0); |
| 48 | + println!("b: {b}"); |
| 49 | + let b2 = b; |
| 50 | + println!("b2: {b2}"); |
| 51 | + // works at this line |
| 52 | + move_a_box(b2); |
| 53 | + // but if you include the next line, |
| 54 | + // this will cause compiler error |
| 55 | + move_a_box(b); |
| 56 | + } |
| 57 | + |
| 58 | + ``` |
| 59 | + - rust panic on compile if last line `move_a_box(b);` is kept in: |
| 60 | + - ```rust |
| 61 | + error[E0382]: use of moved value: `b` |
| 62 | + --> exercises/ch04-01-ownership/src/main.rs:12:16 |
| 63 | + | |
| 64 | + 7 | let b = Box::new(0); |
| 65 | + | - move occurs because `b` has type `Box<i32>`, which does not implement the `Copy` trait |
| 66 | + 8 | println!("b: {b}"); |
| 67 | + 9 | let b2 = b; |
| 68 | + | - value moved here |
| 69 | + ... |
| 70 | + 12 | move_a_box(b); |
| 71 | + | ^ value used here after move |
| 72 | + | |
| 73 | + help: consider cloning the value if the performance cost is acceptable |
| 74 | + | |
| 75 | + 9 | let b2 = b.clone(); |
| 76 | + | ++++++++ |
| 77 | + |
| 78 | + For more information about this error, try `rustc --explain E0382`. |
| 79 | + error: could not compile `ch04-01-ownership` (bin "ch04-01-ownership") due to 1 previous error |
| 80 | + ``` |
| 81 | + - See [[Rust Ownership]] for a summary |
| 82 | + - |
38 | 83 | - |
0 commit comments