I am learning Rust from The Rust Programming Language and I am having a WTF moment when code that is not supposed to compile is compiling.
Rust has a rule that you can have only 1 mutable reference to a given data in a particular scope. Which means this code isn't suppose to compile:
fn main() {
let mut s1 = String::from("Hello");
let r1 = &mut s1;
let r2 = &mut s1;
//println!("{} {}", r1, r2);
}
This compiles with no errors!
The expected error only happens when I uncomment the println!
. Why does it let me declare multiple references? Doesn't it make more sense to stop me when I declare multiple mutable references rather than when I use them?
What is going on here? I am using Rust 1.39.0.