Skip to content
Latchkey

Rust E0384 "cannot assign twice to immutable variable" in CI

You assigned a new value to a binding that is immutable. Rust bindings are immutable by default, so a second assignment without mut is rejected.

What this error means

The compiler reports error[E0384]: cannot assign twice to immutable variable total`` at the reassignment. It is deterministic.

cargo
error[E0384]: cannot assign twice to immutable variable `total`
 --> src/sum.rs:4:5
  |
2 |     let total = 0;
  |         ----- first assignment to `total`
...
4 |     total = total + n;
  |     ^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable

Common causes

Reassigning an immutable binding

A let total = 0; is immutable; total = ... later is a second assignment, which Rust forbids without mut.

Intending to shadow instead

If you wanted a fresh binding, you needed let total = ... again, not a bare assignment.

How to fix it

Declare the binding mut

Make the variable mutable if it is genuinely meant to change.

src/sum.rs
let mut total = 0;
for n in nums { total += n; }

Shadow with a new let

If each value is a distinct binding, rebind with let.

src/sum.rs
let total = compute_base();
let total = total + adjustment;

How to prevent it

  • Use mut for accumulators and counters that change in place.
  • Use shadowing (let x = ... again) for staged transformations.
  • Rely on rust-analyzer to catch the reassignment early.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →