panic: already borrowed: BorrowMutError Rust Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

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).

Exact message you may see 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
Seen on: Rust

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 calling borrow_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

  1. Limit guard scope with blocks: { let mut x = cell.borrow_mut(); ... }
  2. try_borrow_mut() to handle failures
  3. Use Cell/Mutex appropriate to the use case

Detailed fix by platform

Rust

  1. let value = { cell.borrow().clone() }; cell.borrow_mut().push(value);

How to diagnose

  1. Guards — Overlapping?
  2. Reentrancy — Callback?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

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 (Rust)E0499: cannot borrow …panic: attempt to divide by zero — Rust: thread 'main' panicked at src/main.rs:3:13: attempt to divide by zero (Rust)panic: attempt to div…panic: attempt to subtract with overflow — Rust: thread 'main' panicked at src/main.rs:4:13: attempt to subtract with overflow (Rust)panic: attempt to sub…panic: called Option::unwrap() on a None value — Rust: thread 'main' panicked at src/main.rs:4:20: called `Option::unwrap()` on a `None` value (Rust)panic: called Option:…panic: called Result::unwrap() on an Err value — Rust: thread 'main' panicked at src/main.rs:5:36: called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit } (Rust)panic: called Result:…panic: index out of bounds: the len is N but the index is M — Rust: thread 'main' panicked at src/main.rs:5:5: index out of bounds: the len is 3 but the index is 3 (Rust)panic: index out of b…panic: runtime error: index out of range — Go: panic: runtime error: index out of range [3] with length 3 (Go)panic: runtime error:…panic: runtime error: integer divide by zero — Go: panic: runtime error: integer divide by zero (Go)panic: runtime error:…panic: al…

Explore the map of this error Full error map →

🧠 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.