-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Borrow checker allows multiple mutable references in the same scope (unexpected behavior) #138140
Comments
This is intentional. You can read more about the problem and the solution in the Non-Lexical Lifetimes RFC - https://rust-lang.github.io/rfcs/2094-nll.html (technically, NLL only partially shipped in 1.35.0. The full release was in 1.65.0) |
Also note this bit in rust book, which explains this (https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references):
let mut s = String::from("hello");
let r1 = &s; // no problem
let r2 = &s; // no problem
println!("{r1} and {r2}");
// variables r1 and r2 will not be used after this point
let r3 = &mut s; // no problem
println!("{r3}");
|
Thank you. |
Accoring to Rust’s borrowing rules, there shouldn't be more than one mutable reference in the same scope, but in the followin code it actually compiled and executed successfully.
I tried this code:
I expected to see this happen: *error[E0499]: cannot borrow
x
as mutable more than once at a time*
Instead, this happened: it actually compiled and executed successfully,and only raises two warnings of "warning: unused variable"
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: