diff options
author | Emerentius <emerentius@arcor.de> | 2019-09-01 11:54:40 +0200 |
---|---|---|
committer | Emerentius <emerentius@arcor.de> | 2019-09-01 11:54:40 +0200 |
commit | fdd278fde1fc7e5c26a853200cade201bb86e48f (patch) | |
tree | d2085f2938a6d0421e8cafb849c4fd23e203bb56 /rust.html.markdown | |
parent | c5f6427b6001aa0bb260d1636641c5edf01d8998 (diff) |
rust: update explanation of borrow system
With the introduction of non-lexical lifetimes, borrows no longer last
until the end of scope. This has always been active in the 2018 edition
and is now also true in the 2015 edition as of Rust 1.36
Diffstat (limited to 'rust.html.markdown')
-rw-r--r-- | rust.html.markdown | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/rust.html.markdown b/rust.html.markdown index f8f6c5e4..92794e69 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -288,7 +288,7 @@ fn main() { // Reference – an immutable pointer that refers to other data // When a reference is taken to a value, we say that the value has been ‘borrowed’. // While a value is borrowed immutably, it cannot be mutated or moved. - // A borrow lasts until the end of the scope it was created in. + // A borrow is active until the last use of the borrowing variable. let mut var = 4; var = 3; let ref_var: &i32 = &var; @@ -297,6 +297,8 @@ fn main() { println!("{}", *ref_var); // var = 5; // this would not compile because `var` is borrowed // *ref_var = 6; // this would not either, because `ref_var` is an immutable reference + ref_var; // no-op, but counts as a use and keeps the borrow active + var = 2; // ref_var is no longer used after the line above, so the borrow has ended // Mutable reference // While a value is mutably borrowed, it cannot be accessed at all. @@ -307,6 +309,7 @@ fn main() { println!("{}", *ref_var2); // 6 , // var2 would not compile. // ref_var2 is of type &mut i32, so stores a reference to an i32, not the value. // var2 = 2; // this would not compile because `var2` is borrowed. + ref_var2; // no-op, but counts as a use and keeps the borrow active until here } ``` |