Rust panics with RefCell already mutably borrowed (BorrowError)
Fix it fast
Most common cause, 1 of 3A 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.
{
let mut state = cell.borrow_mut();
state.count += 1;
} // RefMut dropped here
let total = cell.borrow().count; // no panic
// or: drop(state); before the readShort 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.
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).
RUST_BACKTRACE=1 cargo runThen find the line you see in this list and open its fix:
- 1A borrow_mut() guard stored in a variable is still in scope
The same function holds let mut x = cell.borrow_mut(); and, a few lines later, calls cell.borrow() or a helper that does - 2A temporary guard in a match or if let scrutinee lives through the arms
The panic line sits inside a match arm or an else block, and the expression after match or if let calls borrow_mut() directly - 3A callback or second method borrows the same cell while it is being mutated
The 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 borrowedalready mutably borrowed: BorrowErrorRefCell already borrowedalready 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.
grep -n "borrow_mut()" src/*.rsFix
Finish the mutation inside its own block, or call drop on the guard, before any read of the same cell.
{
let mut state = cell.borrow_mut();
state.count += 1;
} // RefMut dropped here
let total = cell.borrow().count; // no panic
// or: drop(state); before the readVerify
The program runs past the old panic line, and RUST_BACKTRACE output no longer shows a panic in core::cell.
cargo runConfirmed by doc.rust-lang.org
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.
grep -nE "(match|if let).*borrow_mut\(\)" src/*.rsFix
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.
// 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.
cargo testConfirmed by doc.rust-lang.org
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.
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.
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.
RUST_BACKTRACE=1 cargo runConfirmed by doc.rust-lang.org
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.