From d5b61effcb4219c914cbf15e50c894bed6ed7b46 Mon Sep 17 00:00:00 2001 From: Andrew Pritchard Date: Sat, 5 Oct 2019 00:29:21 +1000 Subject: [PATCH 1/2] Updated aliasing for nll --- src/scope/borrow/alias.md | 71 ++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/src/scope/borrow/alias.md b/src/scope/borrow/alias.md index 2a12e605f4..59255ed700 100644 --- a/src/scope/borrow/alias.md +++ b/src/scope/borrow/alias.md @@ -1,9 +1,9 @@ # Aliasing Data can be immutably borrowed any number of times, but while immutably -borrowed, the original data can't be mutably borrowed. On the other hand, -only *one* mutable borrow is allowed at a time. The original data can be -borrowed again only *after* the mutable reference goes out of scope. +borrowed, the original data can't be mutably borrowed. On the other hand, only +*one* mutable borrow is allowed at a time. The original data can be borrowed +again only *after* the mutable reference has been used for the last time. ```rust,editable struct Point { x: i32, y: i32, z: i32 } @@ -11,49 +11,50 @@ struct Point { x: i32, y: i32, z: i32 } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; - { - let borrowed_point = &point; - let another_borrow = &point; + let borrowed_point = &point; + let another_borrow = &point; - // Data can be accessed via the references and the original owner - println!("Point has coordinates: ({}, {}, {})", - borrowed_point.x, another_borrow.y, point.z); + // Data can be accessed via the references and the original owner + println!("Point has coordinates: ({}, {}, {})", + borrowed_point.x, another_borrow.y, point.z); - // Error! Can't borrow `point` as mutable because it's currently - // borrowed as immutable. - //let mutable_borrow = &mut point; - // TODO ^ Try uncommenting this line + // Error! Can't borrow `point` as mutable because it's currently + // borrowed as immutable. + // let mutable_borrow = &mut point; + // TODO ^ Try uncommenting this line - // Immutable references go out of scope - } + // The borrowed values are used again here + println!("Point has coordinates: ({}, {}, {})", + borrowed_point.x, another_borrow.y, point.z); - { - let mutable_borrow = &mut point; + // The immutable references are no longer used for the rest of the code so + // it is possible to reborrow with a mutbale reference. + let mutable_borrow = &mut point; - // Change data via mutable reference - mutable_borrow.x = 5; - mutable_borrow.y = 2; - mutable_borrow.z = 1; + // Change data via mutable reference + mutable_borrow.x = 5; + mutable_borrow.y = 2; + mutable_borrow.z = 1; - // Error! Can't borrow `point` as immutable because it's currently - // borrowed as mutable. - //let y = &point.y; - // TODO ^ Try uncommenting this line + // Error! Can't borrow `point` as immutable because it's currently + // borrowed as mutable. + // let y = &point.y; + // TODO ^ Try uncommenting this line - // Error! Can't print because `println!` takes an immutable reference. - //println!("Point Z coordinate is {}", point.z); - // TODO ^ Try uncommenting this line + // Error! Can't print because `println!` takes an immutable reference. + // println!("Point Z coordinate is {}", point.z); + // TODO ^ Try uncommenting this line - // Ok! Mutable references can be passed as immutable to `println!` - println!("Point has coordinates: ({}, {}, {})", - mutable_borrow.x, mutable_borrow.y, mutable_borrow.z); + // Ok! Mutable references can be passed as immutable to `println!` + println!("Point has coordinates: ({}, {}, {})", + mutable_borrow.x, mutable_borrow.y, mutable_borrow.z); - // Mutable reference goes out of scope - } + // The mutable reference is no longer used for the rest of the code so it + // is possible to reborrow. // Immutable references to `point` are allowed again - let borrowed_point = &point; + let new_borrowed_point = &point; println!("Point now has coordinates: ({}, {}, {})", - borrowed_point.x, borrowed_point.y, borrowed_point.z); + new_borrowed_point.x, new_borrowed_point.y, new_borrowed_point.z); } ``` From 6b8d6aeceac3230899b32f670a6691938bee651a Mon Sep 17 00:00:00 2001 From: Andrew Pritchard Date: Sat, 5 Oct 2019 00:34:54 +1000 Subject: [PATCH 2/2] typos --- src/scope/borrow/alias.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/scope/borrow/alias.md b/src/scope/borrow/alias.md index 59255ed700..9cfd5fe5dc 100644 --- a/src/scope/borrow/alias.md +++ b/src/scope/borrow/alias.md @@ -28,7 +28,7 @@ fn main() { borrowed_point.x, another_borrow.y, point.z); // The immutable references are no longer used for the rest of the code so - // it is possible to reborrow with a mutbale reference. + // it is possible to reborrow with a mutable reference. let mutable_borrow = &mut point; // Change data via mutable reference @@ -50,9 +50,7 @@ fn main() { mutable_borrow.x, mutable_borrow.y, mutable_borrow.z); // The mutable reference is no longer used for the rest of the code so it - // is possible to reborrow. - - // Immutable references to `point` are allowed again + // is possible to reborrow let new_borrowed_point = &point; println!("Point now has coordinates: ({}, {}, {})", new_borrowed_point.x, new_borrowed_point.y, new_borrowed_point.z);