Skip to content

Commit d5b61ef

Browse files
committed
Updated aliasing for nll
1 parent e60b98d commit d5b61ef

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

src/scope/borrow/alias.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
11
# Aliasing
22

33
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.
77

88
```rust,editable
99
struct Point { x: i32, y: i32, z: i32 }
1010
1111
fn main() {
1212
let mut point = Point { x: 0, y: 0, z: 0 };
1313
14-
{
15-
let borrowed_point = &point;
16-
let another_borrow = &point;
14+
let borrowed_point = &point;
15+
let another_borrow = &point;
1716
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);
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);
2120
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
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
2625
27-
// Immutable references go out of scope
28-
}
26+
// The borrowed values are used again here
27+
println!("Point has coordinates: ({}, {}, {})",
28+
borrowed_point.x, another_borrow.y, point.z);
2929
30-
{
31-
let mutable_borrow = &mut point;
30+
// The immutable references are no longer used for the rest of the code so
31+
// it is possible to reborrow with a mutbale reference.
32+
let mutable_borrow = &mut point;
3233
33-
// Change data via mutable reference
34-
mutable_borrow.x = 5;
35-
mutable_borrow.y = 2;
36-
mutable_borrow.z = 1;
34+
// Change data via mutable reference
35+
mutable_borrow.x = 5;
36+
mutable_borrow.y = 2;
37+
mutable_borrow.z = 1;
3738
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
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
4243
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
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
4647
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);
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);
5051
51-
// Mutable reference goes out of scope
52-
}
52+
// The mutable reference is no longer used for the rest of the code so it
53+
// is possible to reborrow.
5354
5455
// Immutable references to `point` are allowed again
55-
let borrowed_point = &point;
56+
let new_borrowed_point = &point;
5657
println!("Point now has coordinates: ({}, {}, {})",
57-
borrowed_point.x, borrowed_point.y, borrowed_point.z);
58+
new_borrowed_point.x, new_borrowed_point.y, new_borrowed_point.z);
5859
}
5960
```

0 commit comments

Comments
 (0)