about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2015-05-10 16:44:22 -0400
committerSteve Klabnik <steve@steveklabnik.com>2015-05-10 16:44:22 -0400
commitba8eb58257bf1fa3d0ee8fa0d4f6d2ac1add3818 (patch)
tree70231d4edc9450ed5f74fcdfac004c9b3c902f09
parenta7253780b2d58b0f7740e9ad0867cffddec460d0 (diff)
parent25543f38e437f959298238790a3737ff44ab5baf (diff)
downloadrust-ba8eb58257bf1fa3d0ee8fa0d4f6d2ac1add3818.tar.gz
rust-ba8eb58257bf1fa3d0ee8fa0d4f6d2ac1add3818.zip
Rollup merge of #25222 - GuillaumeGomez:doc-ref, r=steveklabnik
r? @steveklabnik 
-rw-r--r--src/doc/trpl/references-and-borrowing.md35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/doc/trpl/references-and-borrowing.md b/src/doc/trpl/references-and-borrowing.md
index 8bb3f94760b..da416e994c4 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;
          ^
@@ -334,3 +335,37 @@ 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:
+
+```text
+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);
+}
+```
\ No newline at end of file