Home › Rust

error[E0384]: cannot assign twice to immutable variable (Rust) — how to fix

error[E0384]: cannot assign twice to immutable variable `count`
 --> src/main.rs:4:5
  |
2 |     let count = 0;
  |         ----- first assignment to `count`
3 |
4 |     count += 1;
  |     ^^^^^^^^^^ cannot assign twice to immutable variable
  |
help: consider making this binding mutable
  |
2 |     let mut count = 0;
  |         +++

Quick fix: Declare the binding as mutable: let mut count = 0;. For function parameters write fn f(mut x: i32); for pattern bindings let (mut a, b) = ... or for mut x in .... Or shadow it with a new let if you don't need mutation.

What it means

let x = 5; creates an immutable binding. Any later x = ..., x += ... or similar is rejected at compile time with E0384. The compiler's help: line almost always shows the exact fix.

The related error for references is E0596 (cannot borrow x as mutable, as it is not declared as mutable), which happens when you call a &mut self method like v.push(1) on a non-mut binding. The fix is the same: add mut.

Fix 1: add mut

fn main() {
    let mut count = 0;
    count += 1;
    println!("{count}");
}

Cause 2: function parameters

Parameters are immutable bindings too:

fn shout(s: String) {
    s.push('!');      // E0596
    s = s.to_uppercase();  // E0384
}
fn shout(mut s: String) -> String {
    s.push('!');
    s = s.to_uppercase();
    s
}

mut on a parameter is local to the function; callers aren't affected.

Cause 3: loop variables and patterns

for x in 0..3 {
    x *= 2;           // E0384
}
for mut x in 0..3 {
    x *= 2;
    println!("{x}");
}

Each binding in a pattern needs its own mut:

let (mut a, b) = (1, 2);
a += b;
if let Some(mut v) = maybe { v += 1; }

Cause 4: deferred initialization

Declaring first and assigning later is allowed once without mut:

let x;
if cond { x = 1; } else { x = 2; }   // fine: each path assigns once

But assigning in a loop, or on two paths that can both run, is E0384:

let total;
for n in nums {
    total = n;        // may assign more than once
}

Use let mut total = 0;, or better compute it as an expression:

let total: i32 = nums.iter().sum();
let x = if cond { 1 } else { 2 };

Alternative: shadowing

If you only need a transformed value, rebind with a new let instead of mutating:

let input = "  42 ";
let input = input.trim();
let input: i32 = input.parse().unwrap();

Shadowing can even change the type, which mut cannot.

Inside closures and structs

Last updated 2026-11-17.