# Rust: thread 'main' panicked at src/main.rs:8:9: already borrowed: BorrowMutError / already mutably borrowed: BorrowError

**Code:** `panic: already borrowed: BorrowMutError` · **Category:** Rust · **Seen on:** Rust

A `RefCell` was borrowed in conflicting ways at runtime (a mutable borrow while another borrow was alive).

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/rust/panic-already-borrowed-refcell)
