From 11bedee95a50f5dad8bd5cf85aa9a78eeb7d1c7e Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 3 Mar 2019 15:01:25 -0500 Subject: [PATCH 1/3] Add an example that illustrates NLL --- src/ch04-02-references-and-borrowing.md | 29 +++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/ch04-02-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md index eb41161597..fd981cc423 100644 --- a/src/ch04-02-references-and-borrowing.md +++ b/src/ch04-02-references-and-borrowing.md @@ -229,10 +229,31 @@ from under them! However, multiple immutable references are okay because no one who is just reading the data has the ability to affect anyone else’s reading of the data. -Even though these errors may be frustrating at times, remember that it’s the -Rust compiler pointing out a potential bug early (at compile time rather than -at runtime) and showing you exactly where the problem is. Then you don’t have -to track down why your data isn’t what you thought it was. +Note that a reference's scope starts from where it is introduced and continues +through the last time that reference is used. For instance, this code will +compile because the last usage of the immutable references occurs before the +mutable reference is introduced: + +```rust +let mut s = String::from("hello"); + +let r1 = &s; // no problem +let r2 = &s; // no problem +println!("{} and {}", r1, r2); +// r1 and r2 are no longer used after this point + +let r3 = &mut s; // no problem +println!("{}", r3); +``` + +The scopes of the immutable references `r1` and `r2` end after the `println!` +where they are last used, which is before the mutable reference `r3` is +created. These scopes don't overlap, so this code is allowed. + +Even though borrowing errors may be frustrating at times, remember that it’s +the Rust compiler pointing out a potential bug early (at compile time rather +than at runtime) and showing you exactly where the problem is. Then you don’t +have to track down why your data isn’t what you thought it was. ### Dangling References From fa6519e87fabfa78757c2b39f371f9f3de5cd407 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Fri, 8 Mar 2019 08:27:08 -0500 Subject: [PATCH 2/3] Specify edition2018 --- src/ch04-02-references-and-borrowing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch04-02-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md index fd981cc423..bfc1d3b52c 100644 --- a/src/ch04-02-references-and-borrowing.md +++ b/src/ch04-02-references-and-borrowing.md @@ -234,7 +234,7 @@ through the last time that reference is used. For instance, this code will compile because the last usage of the immutable references occurs before the mutable reference is introduced: -```rust +```rust,edition2018 let mut s = String::from("hello"); let r1 = &s; // no problem From 98e4db374bf489b75cc8a0627f0087074b266c38 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 10 Mar 2019 11:09:50 -0400 Subject: [PATCH 3/3] Temporarily ignore an example because of a rustdoc bug Should be fixed in rust 1.35 --- src/ch04-02-references-and-borrowing.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ch04-02-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md index bfc1d3b52c..b44aad3e50 100644 --- a/src/ch04-02-references-and-borrowing.md +++ b/src/ch04-02-references-and-borrowing.md @@ -234,7 +234,11 @@ through the last time that reference is used. For instance, this code will compile because the last usage of the immutable references occurs before the mutable reference is introduced: -```rust,edition2018 + + +```rust,edition2018,ignore let mut s = String::from("hello"); let r1 = &s; // no problem