Rust: thread 'main' panicked at src/main.rs:8:9: already borrowed: BorrowMutError / already mutably borrowed: BorrowError
A RefCell was borrowed in conflicting ways at runtime (a mutable borrow while another borrow was alive).
thread 'main' panicked at src/main.rs:8:9: already borrowed: BorrowMutErroralready mutably borrowed: BorrowErroralready borrowed: BorrowMutErrorRefCell already borrowed panicthread 'main' panicked: already mutably borrowed Meaning
RefCell enforces borrow rules at runtime. Keep borrows short (drop the Ref/RefMut before borrowing again), avoid re-entrant calls that borrow the same cell, use try_borrow_mut to detect, or restructure data (use Cell for Copy types, split state, or Rc<RefCell<T>> carefully). In multithreaded code use Mutex/RwLock.
Common causes
- Holding
borrow()while callingborrow_mut()on the same cell - Callbacks re-entering code that borrows
- Iterating while mutating through RefCell
- Long-lived guards stored in variables
- Recursive structure access
⚡ Quick fix
- Limit guard scope with blocks:
{ let mut x = cell.borrow_mut(); ... } try_borrow_mut()to handle failures- Use
Cell/Mutexappropriate to the use case
Detailed fix by platform
Rust
let value = { cell.borrow().clone() }; cell.borrow_mut().push(value);
How to diagnose
- Guards — Overlapping?
- Reentrancy — Callback?
- Alternative — Cell/Mutex?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- E0499: cannot borrow as mutable more than once at a time Rust: error[E0499]: cannot borrow `v` as mutable more than once at a time
- panic: attempt to divide by zero Rust: thread 'main' panicked at src/main.rs:3:13: attempt to divide by zero
- panic: attempt to subtract with overflow Rust: thread 'main' panicked at src/main.rs:4:13: attempt to subtract with overflow
Most viewed in Rust
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026