about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2015-05-08 23:54:24 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2015-05-08 23:54:24 +0200
commit715f7c3cd2af3d54ef79d75fe7b1e820853f08ea (patch)
tree831175795a8ee1b4c2d661928414ab921dda2ef4
parent7132092ce6e954eb58d490fa886d0865c5cfba38 (diff)
downloadrust-715f7c3cd2af3d54ef79d75fe7b1e820853f08ea.tar.gz
rust-715f7c3cd2af3d54ef79d75fe7b1e820853f08ea.zip
Add a precision for references
-rw-r--r--src/doc/trpl/references-and-borrowing.md32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/doc/trpl/references-and-borrowing.md b/src/doc/trpl/references-and-borrowing.md
index 21feff73342..06ff9bfbc55 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);
+}