Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Rust panics with RefCell already mutably borrowed (BorrowError)

Your error

RefCell already mutably borrowed3 more lines it can show

Fix it fast

Most common cause, 1 of 3

A borrow_mut() guard stored in a variable is still in scope

Finish the mutation inside its own block, or call drop on the guard, before any read of the same cell.

Fix
{
    let mut state = cell.borrow_mut();
    state.count += 1;
} // RefMut dropped here

let total = cell.borrow().count; // no panic
// or: drop(state); before the read
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

The panic RefCell already mutably borrowed (already mutably borrowed: BorrowError before Rust 1.89) means borrow() ran while a RefMut from borrow_mut() on the same cell was still alive. RefCell checks this at runtime. End the guard's scope with a block or drop(), copy values out of match scrutinees, and avoid callbacks while holding it. Applies to Rust 1.100.

  • Applies to Rust 1.100 (stable, Nov 2026), Rust 2021 and 2024 editions
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run the program with a backtrace. The panic location is the borrow() line in your code, because borrow is marked track_caller. If the frames above it go through a closure or a second method on the same object, look at cause 3; if not, the guard lives in the same function (cause 1 or 2).

Run this first
RUST_BACKTRACE=1 cargo run

Then find the line you see in this list and open its fix:

  1. 1A borrow_mut() guard stored in a variable is still in scopeThe same function holds let mut x = cell.borrow_mut(); and, a few lines later, calls cell.borrow() or a helper that does
  2. 2A temporary guard in a match or if let scrutinee lives through the armsThe panic line sits inside a match arm or an else block, and the expression after match or if let calls borrow_mut() directly
  3. 3A callback or second method borrows the same cell while it is being mutatedThe backtrace shows your borrow_mut() call, then a closure, observer or trait method, then the borrow() that panics, often on a shared Rc<RefCell<T>>

What does the error look like?

  • RefCell already mutably borrowed
  • already mutably borrowed: BorrowError
  • RefCell already borrowed
  • already borrowed: BorrowMutError

Cause 1 of 3

A borrow_mut() guard stored in a variable is still in scope

You seeThe same function holds let mut x = cell.borrow_mut(); and, a few lines later, calls cell.borrow() or a helper that does

Check

RefCell already mutably borrowed is raised here because a RefMut kept in a local variable stays active until the end of its block. The standard library docs state that the value cannot be borrowed while that RefMut is active, and they show this exact pattern as the panic example for borrow.

Check
grep -n "borrow_mut()" src/*.rs

Fix

Finish the mutation inside its own block, or call drop on the guard, before any read of the same cell.

Fix
{
    let mut state = cell.borrow_mut();
    state.count += 1;
} // RefMut dropped here

let total = cell.borrow().count; // no panic
// or: drop(state); before the read

Verify

The program runs past the old panic line, and RUST_BACKTRACE output no longer shows a panic in core::cell.

Verify
cargo run

Confirmed by doc.rust-lang.org

Did this fix it?

Cause 2 of 3

A temporary guard in a match or if let scrutinee lives through the arms

You seeThe panic line sits inside a match arm or an else block, and the expression after match or if let calls borrow_mut() directly

Check

A temporary RefMut created in the head of a match lives until the whole match ends, because the Rust reference says a match scrutinee is not a temporary scope. Before the 2024 edition the same is true for if let, where the guard survives into the else block; edition 2024 drops it before else.

Check
grep -nE "(match|if let).*borrow_mut\(\)" src/*.rs

Fix

Read what you need into a local first, so the guard dies at the end of that statement. Edition 2024 also shortens the if let case; cargo fix --edition helps migrate.

Fix
// before: match cell.borrow_mut().pop() { None => cell.borrow().len(), .. }
let next = cell.borrow_mut().pop(); // guard dropped at the semicolon
match next {
    None => println!("empty, len {}", cell.borrow().len()),
    Some(v) => println!("got {v}"),
}

Verify

Both arms run without a panic, including the one that reads the cell again.

Verify
cargo test

Confirmed by doc.rust-lang.org

Did this fix it?

Cause 3 of 3

A callback or second method borrows the same cell while it is being mutated

You seeThe backtrace shows your borrow_mut() call, then a closure, observer or trait method, then the borrow() that panics, often on a shared Rc<RefCell<T>>

Check

Re-entrancy causes this panic when code holding borrow_mut() hands control to something that also owns a clone of the same Rc and reads it. Each method looks fine alone, so only the backtrace reveals the nesting.

Check
RUST_BACKTRACE=1 cargo run 2>&1 | grep -n "closure\|RefCell"

Fix

Collect what the callback needs, release the guard, then call the callback. Where nesting is expected, use try_borrow, which returns an Err instead of panicking, and handle the busy case.

Fix
let listeners = {
    let mut s = shared.borrow_mut();
    s.value = 42;
    s.listeners.clone()
}; // guard released before callbacks run
for f in listeners {
    f(&shared);
}

if let Ok(s) = shared.try_borrow() {
    println!("{}", s.value);
}

Verify

The callbacks run and can read the shared value, and try_borrow returns Ok outside the mutation.

Verify
RUST_BACKTRACE=1 cargo run

Confirmed by doc.rust-lang.org

Did this fix it?

How do I stop it coming back?

  • Keep each borrow_mut() inside the smallest block that needs it, and never store a RefMut in a struct field.
  • Do not call closures, trait objects or event handlers while holding a RefMut on shared state.
  • Add a unit test that drives the re-entrant path (callback that reads state) so a new nesting panics in CI, not in production.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.