Home › Rust

error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable (Rust) — how to fix

error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable
 --> src/main.rs:3:5
  |
3 |     v.push(4);
  |     ^ cannot borrow as mutable
  |
help: consider changing this to be mutable
  |
2 |     let mut v = vec![1, 2, 3];
  |         +++

Quick fix: Declare the variable mutable: let mut v = Vec::new();. For function parameters take &mut T (fn add(v: &mut Vec<i32>)) and pass &mut v. In methods use &mut self. For a closure that mutates captured state, write let mut f = || .... Behind Rc/Arc use RefCell/Mutex.

What it means

Methods like Vec::push, String::push_str, HashMap::insert and Iterator::next take &mut self. To call them, Rust needs a mutable borrow, and you can only take one from something that is mutable. E0596 appears in a few forms:

Fix 1: let mut

let mut v = vec![1, 2, 3];
v.push(4);

let mut it = v.iter();
it.next();               // next() needs &mut self too

The same goes for pattern bindings: for mut x in ..., if let Some(mut s) = opt, let (mut a, b) = pair;.

Fix 2: function parameters

A parameter taken by value is immutable inside the function unless marked mut:

fn add_one(mut v: Vec<i32>) -> Vec<i32> {
    v.push(1);
    v
}

If the caller should see the change, take a mutable reference instead and pass one:

fn add_one(v: &mut Vec<i32>) {
    v.push(1);
}

let mut nums = vec![];
add_one(&mut nums);

Passing &nums to a function that wants &mut Vec<i32> gives E0308 (mismatched types); writing &mut nums when nums isn't mut gives E0596.

Fix 3: methods with &self

struct Cart { items: Vec<String> }

impl Cart {
    fn add(&self, item: String) {
        self.items.push(item);   // E0596: behind a `&` reference
    }
}

Change the receiver to &mut self, and make sure the caller's binding is mutable:

impl Cart {
    fn add(&mut self, item: String) {
        self.items.push(item);
    }
}

let mut cart = Cart { items: vec![] };
cart.add("apple".into());

Fix 4: iterating and mutating elements

for x in v.iter() { *x += 1; }       // error: `x` is a `&` reference

Use a mutable iterator:

for x in v.iter_mut() { *x += 1; }
// or
for x in &mut v { *x += 1; }

The same with HashMap: use get_mut, values_mut or entry(...) instead of get.

Fix 5: closures that mutate captured variables

A closure that changes captured state is FnMut, and calling it requires a mutable binding:

let mut count = 0;
let mut inc = || count += 1;   // needs `mut inc`
inc();
inc();

If a function takes impl Fn() and you pass a mutating closure you get a different error; change the bound to impl FnMut().

Fix 6: shared ownership (Rc, Arc)

Rc<T> and Arc<T> only give shared (&) access, so let mut doesn't help. Use interior mutability:

use std::cell::RefCell;
use std::rc::Rc;

let shared = Rc::new(RefCell::new(Vec::new()));
shared.borrow_mut().push(1);

Across threads use Arc<Mutex<T>>: data.lock().unwrap().push(1);

Last updated 2026-11-29.