From 715f7c3cd2af3d54ef79d75fe7b1e820853f08ea Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 8 May 2015 23:54:24 +0200 Subject: [PATCH 1/2] Add a precision for references --- src/doc/trpl/references-and-borrowing.md | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/doc/trpl/references-and-borrowing.md b/src/doc/trpl/references-and-borrowing.md index 21feff73342ce..06ff9bfbc55d9 100644 --- a/src/doc/trpl/references-and-borrowing.md +++ b/src/doc/trpl/references-and-borrowing.md @@ -334,3 +334,35 @@ In other words, `y` is only valid for the scope where `x` exists. As soon as `x` goes away, it becomes invalid to refer to it. As such, the error says that the borrow ‘doesn’t live long enough’ because it’s not valid for the right amount of time. + +The same problem occurs when the reference is declared _before_ the variable it refers to: + +```rust,ignore +let y: &i32; +let x = 5; +y = &x; + +println!("{}", y); +``` + +We get this error: + +error: `x` does not live long enough +y = &x; + ^ +note: reference must be valid for the block suffix following statement 0 at +2:16... + let y: &i32; + let x = 5; + y = &x; + + println!("{}", y); +} + +note: ...but borrowed value is only valid for the block suffix following +statement 1 at 3:14 + let x = 5; + y = &x; + + println!("{}", y); +} From 25543f38e437f959298238790a3737ff44ab5baf Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 10 May 2015 21:05:04 +0200 Subject: [PATCH 2/2] Add missing backticks --- src/doc/trpl/references-and-borrowing.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/doc/trpl/references-and-borrowing.md b/src/doc/trpl/references-and-borrowing.md index 06ff9bfbc55d9..82533becef342 100644 --- a/src/doc/trpl/references-and-borrowing.md +++ b/src/doc/trpl/references-and-borrowing.md @@ -312,6 +312,7 @@ println!("{}", y); We get this error: +```text error: `x` does not live long enough y = &x; ^ @@ -347,6 +348,7 @@ println!("{}", y); We get this error: +```text error: `x` does not live long enough y = &x; ^ @@ -366,3 +368,4 @@ statement 1 at 3:14 println!("{}", y); } +``` \ No newline at end of file