Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
Trade-offs: Write locks more expensive than Mutex.
Borrow Rules
At any time, you can have either:
├─ Multiple &T (immutable borrows)
└─ OR one &mut T (mutable borrow)
Never both simultaneously
Error Code Quick Reference
Code
Meaning
Don't Say
Ask Instead
E0596
Cannot get mutable reference
"add mut"
Does this really need mutability?
E0499
Multiple mutable borrows conflict
"split borrows"
Is data structure design correct?
E0502
Borrow conflict
"separate scopes"
Why both borrows needed simultaneously?
RefCell panic
Runtime borrow error
"use try_borrow"
Is runtime checking appropriate?
Workflow
Step 1: Choose Mutability Strategy
Single-threaded?
Need &mut from &self?
→ RefCell<T>
Copy type?
→ Cell<T>
Otherwise?
→ &mut T
Multi-threaded?
Simple atomic?
→ AtomicU64/AtomicBool
Complex data?
Read-heavy → RwLock<T>
Write-heavy → Mutex<T>
Step 2: Handle Borrow Conflicts
E0499 (multiple mut borrows)?
→ Split struct into smaller pieces
→ Use Cell/RefCell for interior mutability
→ Redesign to avoid simultaneous access
E0502 (borrow conflict)?
→ Minimize borrow scopes
→ Clone data if needed
→ Restructure code flow
Step 3: Consider Trade-offs
RefCell?
✅ Flexible
❌ Runtime panics possible
→ Use in prototypes, single-threaded
Mutex?
✅ Thread-safe
❌ Lock contention
→ Profile before optimizing
RwLock?
✅ Many readers efficient
❌ Writer starvation possible
→ Use when reads >> writes
Thread-Safe Selection
Atomic Types
use std::sync::atomic::{AtomicU64, Ordering};
letcounter = AtomicU64::new(0);
counter.fetch_add(1, Ordering::Relaxed);
Use when: Simple counters, flags.
Mutex
use std::sync::Mutex;
letdata = Mutex::new(HashMap::new());
data.lock().unwrap().insert(key, value);
Use when: Thread-safe mutation, balanced read/write.
RwLock
use std::sync::RwLock;
letdata = RwLock::new(HashMap::new());
data.read().unwrap().get(&key); // Many readers
data.write().unwrap().insert(key, value); // Few writers
Use when: Read-heavy workloads (10+ reads per write).