huiali/rust-skills/rust-mutability
Claude Code and agent skill by huiali/rust-skills
- community
Checking that the SKILL.md file is still in the repository.
Interior mutability expert covering Cell, RefCell, Mutex, RwLock patterns, borrow conflicts (E0596, E0499, E0502), and thread-safe mutation strategies.
Install
npx vskill@latest install huiali/rust-skills/rust-mutabilitySKILL.md
Mutability Types
| Type | Controller | Thread-Safe | Use Case | |
|---|---|---|---|---|
&mut T | External caller | Yes | Standard mutable borrow | |
Cell<T> | Interior | No | Copy types with interior mutability | |
RefCell<T> | Interior | No | Non-Copy types with interior mutability | |
Mutex<T> | Interior | Yes | Multi-threaded interior mutability | |
RwLock<T> | Interior | Yes | Multi-threaded read-write lock |
Solution Patterns
Pattern 1: External Mutability
When to use: Default choice, mutability controlled by caller.
Pattern 2: Cell for Copy Types
When to use: Simple values (Copy types) need interior mutability.
Trade-offs: Only works with Copy types, no references.
Pattern 3: RefCell for Non-Copy Types
When to use: Need &mut T from &self, single-threaded.
Trade-offs: Runtime borrow checking, can panic.
Pattern 4: Mutex for Thread Safety
When to use: Multi-threaded interior mutability.
Trade-offs: Lock contention, can deadlock.
Pattern 5: RwLock for Read-Heavy Workloads
When to use: Many readers, few writers.
Trade-offs: Write locks more expensive than Mutex.
Borrow Rules
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
Step 2: Handle Borrow Conflicts
Step 3: Consider Trade-offs
Thread-Safe Selection
Atomic Types
Use when: Simple counters, flags.
Mutex
Use when: Thread-safe mutation, balanced read/write.
RwLock
Use when: Read-heavy workloads (10+ reads per write).
Common Pitfalls
1. Borrow Conflict
Symptom: E0499, E0502 errors
2. RefCell Panic
Symptom: "already borrowed" panic at runtime
3. Lock Held Across Await
Symptom: Deadlock in async code
Review Checklist
When reviewing mutability code:
- [ ] Mutability truly necessary (not premature)
- [ ] Appropriate mutability type chosen (Cell/RefCell/Mutex)
- [ ] RefCell used only in single-threaded contexts
- [ ] Mutex/RwLock used for multi-threaded access
- [ ] Lock scopes minimized to avoid contention
- [ ] No locks held across
.awaitpoints - [ ] Borrow conflicts resolved at design level
- [ ] Runtime panics handled (try_borrow)
- [ ] Atomic types used for simple counters/flags
- [ ] Read-write patterns match RwLock choice
Verification Commands
Advanced Patterns
Splitting Borrows
Interior Mutability with Shared Ownership
Related Skills
- rust-ownership - Ownership and borrowing fundamentals
- rust-concurrency - Thread-safe patterns
- rust-unsafe - UnsafeCell and low-level mutability
- rust-anti-pattern - Mutability anti-patterns
- rust-performance - Lock contention optimization
Localized Reference
- Chinese version: SKILL_ZH.md - 完整中文版本,包含所有内容
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
- semgrepPassed, 100 of 100
- njsscanPassed, 100 of 100
- trufflehogPassed, 100 of 100
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/rust-mutability/SKILL.md
Popularity
- Repository stars
- 23
- Repository forks
- 4
- 7-day trend score
- 16
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 30 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/huiali/rust-skills/rust-mutability)