Skip to main content

E0499: cannot borrow as mutable more than once at a time

A `&mut` reference is exclusive by definition: while it exists, nothing else may touch the value, not even another `&mut`.

Overlapping mutable references are the classic source of aliasing bugs. Rust refuses them at compile time, which is what allows the optimiser to be aggressive with the resulting machine code.

Code that triggers it

fn main() {
    let mut total = 0;
    let a = &mut total;
    let b = &mut total; // error[E0499]
    *a += 1;
    *b += 1;
}

Code that compiles

fn main() {
    let mut total = 0;
    {
        let a = &mut total;
        *a += 1;
    } // first borrow ends here
    let b = &mut total;
    *b += 1;
    println!("{total}");
}

Scope each mutable borrow so they do not overlap, or skip the intermediate references and mutate the variable directly.

Try both versions

The broken version is loaded below so you can see the real compiler message, then fix it yourself.

Your Rust