|
1 | 1 | # Aliasing
|
2 | 2 |
|
3 | 3 | Data can be immutably borrowed any number of times, but while immutably
|
4 |
| -borrowed, the original data can't be mutably borrowed. On the other hand, |
5 |
| -only *one* mutable borrow is allowed at a time. The original data can be |
6 |
| -borrowed again only *after* the mutable reference goes out of scope. |
| 4 | +borrowed, the original data can't be mutably borrowed. On the other hand, only |
| 5 | +*one* mutable borrow is allowed at a time. The original data can be borrowed |
| 6 | +again only *after* the mutable reference has been used for the last time. |
7 | 7 |
|
8 | 8 | ```rust,editable
|
9 | 9 | struct Point { x: i32, y: i32, z: i32 }
|
10 | 10 |
|
11 | 11 | fn main() {
|
12 | 12 | let mut point = Point { x: 0, y: 0, z: 0 };
|
13 | 13 |
|
14 |
| - { |
15 |
| - let borrowed_point = &point; |
16 |
| - let another_borrow = &point; |
17 |
| -
|
18 |
| - // Data can be accessed via the references and the original owner |
19 |
| - println!("Point has coordinates: ({}, {}, {})", |
20 |
| - borrowed_point.x, another_borrow.y, point.z); |
21 |
| -
|
22 |
| - // Error! Can't borrow `point` as mutable because it's currently |
23 |
| - // borrowed as immutable. |
24 |
| - //let mutable_borrow = &mut point; |
25 |
| - // TODO ^ Try uncommenting this line |
26 |
| -
|
27 |
| - // Immutable references go out of scope |
28 |
| - } |
29 |
| -
|
30 |
| - { |
31 |
| - let mutable_borrow = &mut point; |
32 |
| -
|
33 |
| - // Change data via mutable reference |
34 |
| - mutable_borrow.x = 5; |
35 |
| - mutable_borrow.y = 2; |
36 |
| - mutable_borrow.z = 1; |
37 |
| -
|
38 |
| - // Error! Can't borrow `point` as immutable because it's currently |
39 |
| - // borrowed as mutable. |
40 |
| - //let y = &point.y; |
41 |
| - // TODO ^ Try uncommenting this line |
42 |
| -
|
43 |
| - // Error! Can't print because `println!` takes an immutable reference. |
44 |
| - //println!("Point Z coordinate is {}", point.z); |
45 |
| - // TODO ^ Try uncommenting this line |
46 |
| -
|
47 |
| - // Ok! Mutable references can be passed as immutable to `println!` |
48 |
| - println!("Point has coordinates: ({}, {}, {})", |
49 |
| - mutable_borrow.x, mutable_borrow.y, mutable_borrow.z); |
50 |
| -
|
51 |
| - // Mutable reference goes out of scope |
52 |
| - } |
53 |
| -
|
54 |
| - // Immutable references to `point` are allowed again |
55 | 14 | let borrowed_point = &point;
|
| 15 | + let another_borrow = &point; |
| 16 | +
|
| 17 | + // Data can be accessed via the references and the original owner |
| 18 | + println!("Point has coordinates: ({}, {}, {})", |
| 19 | + borrowed_point.x, another_borrow.y, point.z); |
| 20 | +
|
| 21 | + // Error! Can't borrow `point` as mutable because it's currently |
| 22 | + // borrowed as immutable. |
| 23 | + // let mutable_borrow = &mut point; |
| 24 | + // TODO ^ Try uncommenting this line |
| 25 | +
|
| 26 | + // The borrowed values are used again here |
| 27 | + println!("Point has coordinates: ({}, {}, {})", |
| 28 | + borrowed_point.x, another_borrow.y, point.z); |
| 29 | +
|
| 30 | + // The immutable references are no longer used for the rest of the code so |
| 31 | + // it is possible to reborrow with a mutable reference. |
| 32 | + let mutable_borrow = &mut point; |
| 33 | +
|
| 34 | + // Change data via mutable reference |
| 35 | + mutable_borrow.x = 5; |
| 36 | + mutable_borrow.y = 2; |
| 37 | + mutable_borrow.z = 1; |
| 38 | +
|
| 39 | + // Error! Can't borrow `point` as immutable because it's currently |
| 40 | + // borrowed as mutable. |
| 41 | + // let y = &point.y; |
| 42 | + // TODO ^ Try uncommenting this line |
| 43 | +
|
| 44 | + // Error! Can't print because `println!` takes an immutable reference. |
| 45 | + // println!("Point Z coordinate is {}", point.z); |
| 46 | + // TODO ^ Try uncommenting this line |
| 47 | +
|
| 48 | + // Ok! Mutable references can be passed as immutable to `println!` |
| 49 | + println!("Point has coordinates: ({}, {}, {})", |
| 50 | + mutable_borrow.x, mutable_borrow.y, mutable_borrow.z); |
| 51 | +
|
| 52 | + // The mutable reference is no longer used for the rest of the code so it |
| 53 | + // is possible to reborrow |
| 54 | + let new_borrowed_point = &point; |
56 | 55 | println!("Point now has coordinates: ({}, {}, {})",
|
57 |
| - borrowed_point.x, borrowed_point.y, borrowed_point.z); |
| 56 | + new_borrowed_point.x, new_borrowed_point.y, new_borrowed_point.z); |
58 | 57 | }
|
59 | 58 | ```
|
0 commit comments