Why Can't I Have Two Mutable References in Rust?

Rust forbids two mutable references to the same data simultaneously to guarantee memory safety and prevent data races at compile time.

Rust prevents two mutable references to the same data to avoid data races and undefined behavior at compile time. You can only have one mutable reference or multiple immutable references at any given time.

fn main() {
    let mut data = 5;
    let r1 = &mut data;
    // let r2 = &mut data; // This line would cause a compile error
    *r1 = 10;
}