Skip to content
cannot borrow `...` as mutable

cannot borrow `...` as mutable

DodaTech 3 min read

The “cannot borrow ... as mutable” error in Rust means you tried to mutate a value through an immutable reference or exceeded the borrowing rules. Rust’s borrow checker enforces that at any given time you have either one mutable reference or any number of immutable references.

What It Means

Rust’s ownership system ensures memory safety without a garbage collector. The borrow checker tracks references at compile time. If you try to create a mutable reference (&mut) when immutable references already exist, or if the variable itself is not mutable, the compiler rejects your code.

Why It Happens

  • You have an immutable reference (&T) and try to call a method that requires &mut T.
  • You created multiple mutable references to the same data in the same scope.
  • The variable is not declared as mut.
  • You are trying to borrow a field of a struct through an immutable reference to the struct.
  • You have a conflict between borrowing a field and borrowing the whole struct.
  • A function parameter expects &mut T but you passed &T.

How to Fix It

1. Declare variables as mutable

Variables are immutable by default in Rust:

let x = 5;
x += 1; // error: cannot borrow `x` as mutable

// Fix: use let mut
let mut x = 5;
x += 1;

2. Pass mutable references to functions

Functions that modify data need explicit &mut parameters:

fn append_world(s: &mut String) {
    s.push_str(", world!");
}

let mut greeting = String::from("Hello");
append_world(&mut greeting);

3. Ensure no other borrows exist simultaneously

You cannot have both immutable and mutable references in the same scope:

let mut data = vec![1, 2, 3];
let first = &data[0]; // immutable borrow
data.push(4);         // error: cannot borrow `data` as mutable because it is also borrowed as immutable
println!("{}", first);

Fix by limiting the scope of the immutable borrow:

{
    let first = &data[0];
    println!("{}", first);
}
data.push(4); // now this works

4. Use RefCell for interior mutability

When you need to mutate data behind an immutable reference:

use std::cell::RefCell;

let value = RefCell::new(42);
*value.borrow_mut() += 1;
println!("{}", value.borrow());

5. Separate borrows of struct fields

The compiler can track field-level borrows in most cases:

struct Point { x: i32, y: i32 }

let mut p = Point { x: 0, y: 0 };
let x = &p.x;
let y = &mut p.y; // this works since Rust tracks field-level borrows

FAQ

Why does Rust restrict mutable borrows so strictly?
Rust’s borrow checker prevents data races at compile time. Allowing multiple mutable references would let one part of the code modify data while another reads it, causing undefined behavior. This is the same class of bug that plagues C and C++ programs.
What is interior mutability and when should I use it?
Interior mutability lets you mutate data through an immutable reference using types like RefCell, Cell, or Mutex. Use it when you need to mutate data that is behind an &T reference, such as in trait implementations or shared ownership with Rc.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro