- Home
- Skills
- Huiali
- Rust Skills
- Rust Mutability
rust-mutability_skill
- Shell
20
GitHub Stars
3
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill huiali/rust-skills --skill rust-mutability- SKILL_EN.md1.4 KB
- SKILL_ZH.md3.2 KB
- SKILL.md7.9 KB
Overview
This skill is an interior mutability expert for Rust, explaining patterns like Cell, RefCell, Mutex, and RwLock and diagnosing common borrow errors (E0596, E0499, E0502). It guides strategy selection for single-threaded and multi-threaded contexts and shows practical fixes for borrow conflicts and lock misuse. The focus is on correct, performant, and safe mutation in real-world Rust codebases.
How this skill works
The skill inspects code patterns and runtime symptoms to recommend the right mutability primitive or redesign. It maps symptoms (compile errors, runtime RefCell panics, deadlocks) to concrete fixes such as splitting structs, minimizing borrow scopes, using try_borrow, or switching to atomic types. It also prescribes trade-offs and profiling steps for lock-heavy paths.
When to use it
- Default: use &mut T when caller controls mutability and single-threaded ownership suffices.
- Use Cell<T> for interior mutation of small Copy types with &self methods.
- Use RefCell<T> when you need &mut from &self in single-threaded code, accepting runtime borrow checks.
- Use Mutex<T> for interior mutability across threads when writes are frequent or simple to serialize.
- Use RwLock<T> when reads greatly outnumber writes and you need concurrent readers.
Best practices
- Prefer external mutability (&mut) by default; add interior mutability only when caller control is impractical.
- Keep lock scopes minimal and never hold a MutexGuard or RwLockWriteGuard across await points in async code.
- Use Atomic types for simple counters/flags to avoid unnecessary locking.
- When facing E0499/E0502, consider splitting a struct into independent fields or using interior mutability for the conflicting parts.
- Use try_borrow/try_borrow_mut with RefCell to avoid panics and handle borrowing failures gracefully.
Example use cases
- A cached lookup object that needs mutable inserts from &self in a single-threaded context → RefCell<HashMap<..>>.
- A global configuration object with many concurrent readers and occasional writes → RwLock<HashMap<..>>.
- A shared counter across threads where atomic increments suffice → AtomicU64 with fetch_add.
- A small mutable flag inside an immutable API object → Cell<bool> for O(1) interior mutation.
- Refactoring a struct causing E0499 by splitting heavy fields so independent mutable borrows no longer conflict.
FAQ
Replace RefCell with Mutex when the data will be accessed from multiple threads. RefCell enforces borrow rules at runtime but is not Send/Sync; Mutex provides thread-safety with lock semantics and potential contention trade-offs.
How do I stop deadlocks caused by locks and async .await?
Do not hold lock guards across await points. Extract or clone the needed data while holding the lock, drop the guard, then await. Alternatively, redesign to use async-aware synchronization primitives.