I know that the below code does as described in the comments. But why can the Rust compiler
"catch" a potential race condition between 2 variables but not between 3 or more? Thanks!
fn main() {
let mut x = 2;
let y = &mut x; // 1st borrow works
let z = &mut x; // 2nd borrow throws an error
*y += 1;
println!("{}",x);
}
FWIW I'm being pedantic but this is not a race condition -- you would need threads and as soon as you attempt to send the variable across a thread boundary you'd get an error. This is just a generic violation of Rust's "one mutable reference"/noalias rule.