You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letmut s = String::from("hello");let r1 = &s;// no problemlet r2 = &s;// no problemprintln!("{} and {}", r1, r2);// r1 and r2 are no longer used after this pointlet r3 = &mut s;// no problemprintln!("{}", r3);
does not compile. But the description says it should compile
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:
[code example above]
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.
benjamin@benjamin-Lenovo-IdeaPad-Yoga-13:~$ cat a.rs
pub fn main() {
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);
}
benjamin@benjamin-Lenovo-IdeaPad-Yoga-13:~$ rustc a.rs
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
--> a.rs:9:19
|
4 | let r1 = &s; // no problem
| - immutable borrow occurs here
...
9 | let r3 = &mut s; // no problem
| ^ mutable borrow occurs here
10 | println!("{}", r3);
11 | }
| - immutable borrow ends here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.
benjamin@benjamin-Lenovo-IdeaPad-Yoga-13:~$
I guess there should be a block around where r1, r2 is defined? When I changed it to this
letmut s = String::from("hello");{let r1 = &s;// no problemlet r2 = &s;// no problemprintln!("{} and {}", r1, r2);// r1 and r2 are no longer used after this point}let r3 = &mut s;// no problemprintln!("{}", r3);
it compiles without any problem.
The text was updated successfully, but these errors were encountered:
fnmain(){letmut s = String::from("hello");let r1 = &s;// no problemlet r2 = &s;// no problemprintln!("{} and {}", r1, r2);// r1 and r2 are no longer used after this pointlet r3 = &mut s;// no problemprintln!("{}", r3);}
Hi @aiifabbf, this example compiles with the Non-Lexical Lifetimes (NLL) feature. With Rust 1.32, you'll need to opt in to the 2018 edition, either by passing the --edition=2018 flag as @rusty-snake demonstrated, or you can build your project using cargo instead of using rustcas discussed in Chapter 1 and put edition = "2018" in your Cargo.toml.
Hi. I was reading Chaper 4 Section 2: Referencs and Borrowing and just noticed that this code example
does not compile. But the description says it should compile
This code piece is located right above Dangling References section.
My error message
rustc
versionI guess there should be a block around where
r1, r2
is defined? When I changed it to thisit compiles without any problem.
The text was updated successfully, but these errors were encountered: