Rust Memory and State
Use this when the question is really "who owns this, who may mutate it, and
how long must it stay valid?"
Working stance
- Prefer one clear owner and cheap borrowed reads.
- Reach for cloning last, not first.
- If cloning appears mainly to placate the borrow checker, fix the data flow
before keeping the clone.
- Treat
Rc, Arc, RefCell, Mutex, and RwLock as ownership decisions,
not syntax bandages.
- If a lifetime gets hard to express, ask whether the type should own the data.
- Use RAII to make cleanup happen by scope, not by hope.
Decision surface
| Need |
Default move |
| read-only, caller keeps ownership |
borrow &T or &mut T |
| return data past the borrower's scope |
own it |
| cheap duplicated scalar/value type |
Copy or explicit clone |
| shared single-thread ownership |
Rc<T> |
| shared cross-thread ownership |
Arc<T> |
| mutation behind shared access, single-thread |
RefCell<T> |
| mutation behind shared access, multi-thread |
Mutex<T> or RwLock<T> |
| resource cleanup tied to scope |
RAII guard or owning wrapper |
Red flags
- clones multiply each time the borrow checker complains,
Rc or Arc shows up where one owner plus borrowed readers would do,
Arc<Mutex<T>> appears before the real sharing pattern is known,
- references are stored where owning data would simplify the type,
- resource release depends on "remember to call close()",
'static is used to silence a lifetime problem instead of explaining one.
Read borrow-and-own-patterns.md for
API choices, interior-mutability.md for
shared mutation, lifecycle-and-raii.md
for resource scope patterns, and
encapsulation-and-raii.md for
ownership as an architectural lever and the Mutex/MutexGuard/Drop
wireframe.
Source: leynos/rust-skill — distributed by TomeVault.
1---2name: rust-memory-and-state3description: Use for ownership, borrowing, lifetimes, aliasing, smart pointers, interior mutability, resource acquisition is initialisation (RAII), and resource handoff in Rust. Use when this capability is needed.4---56# Rust Memory and State78Use this when the question is really "who owns this, who may mutate it, and9how long must it stay valid?"1011## Working stance1213- Prefer one clear owner and cheap borrowed reads.14- Reach for cloning last, not first.15- If cloning appears mainly to placate the borrow checker, fix the data flow16 before keeping the clone.17- Treat `Rc`, `Arc`, `RefCell`, `Mutex`, and `RwLock` as ownership decisions,18 not syntax bandages.19- If a lifetime gets hard to express, ask whether the type should own the data.20- Use RAII to make cleanup happen by scope, not by hope.2122## Decision surface2324| Need | Default move |25| --- | --- |26| read-only, caller keeps ownership | borrow `&T` or `&mut T` |27| return data past the borrower's scope | own it |28| cheap duplicated scalar/value type | `Copy` or explicit clone |29| shared single-thread ownership | `Rc<T>` |30| shared cross-thread ownership | `Arc<T>` |31| mutation behind shared access, single-thread | `RefCell<T>` |32| mutation behind shared access, multi-thread | `Mutex<T>` or `RwLock<T>` |33| resource cleanup tied to scope | RAII guard or owning wrapper |3435## Red flags3637- clones multiply each time the borrow checker complains,38- `Rc` or `Arc` shows up where one owner plus borrowed readers would do,39- `Arc<Mutex<T>>` appears before the real sharing pattern is known,40- references are stored where owning data would simplify the type,41- resource release depends on "remember to call close()",42- `'static` is used to silence a lifetime problem instead of explaining one.4344Read [borrow-and-own-patterns.md](references/borrow-and-own-patterns.md) for45API choices, [interior-mutability.md](references/interior-mutability.md) for46shared mutation, [lifecycle-and-raii.md](references/lifecycle-and-raii.md)47for resource scope patterns, and48[encapsulation-and-raii.md](references/encapsulation-and-raii.md) for49ownership as an architectural lever and the `Mutex`/`MutexGuard`/`Drop`50wireframe.5152---53> Source: [leynos/rust-skill](https://github.com/leynos/rust-skill) — distributed by [TomeVault](https://tomevault.io).54<!-- tomevault:4.0:skill_md:2026-06-16 -->