rust-ownership
Skill1 starsChanged 42 days agoMITView on GitHub
Rust ownership, borrowing, lifetimes, and smart pointers — the mental model plus concrete fixes for borrow-checker errors. Use when fighting the borrow checker, deciding between T / &T / &mut T, choosing Box/Rc/RefCell/Cow, returning references, writing an RAII/scope guard, or lazily initializing a value. Triggers: borrow checker, cannot borrow, does not live long enough, Rc, RefCell, Cow, RAII, OnceLock, E0382, E0499, E0515, E0597, E0716.
5 other projects use a version of this.
Adopted by
Most recently taken up by
- KeWang0622/clonetaxtheir own version, first seen Nov 29, 2026
- lengrufeng2008/dsh-skill-librarytheir own version, first seen Nov 15, 2026
- adxptived/Rust-Skillstheir own version, first seen Oct 17, 2026
- dawidpereira/rust-skillstheir own version, first seen Mar 22, 2026
- huiali/rust-skillstheir own version, first seen Feb 9, 2026
Other versions5 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 5
- huiali/rust-skills★ 47same name
- adxptived/Rust-Skills★ 1same name
- dawidpereira/rust-skills★ 0same name
- KeWang0622/clonetax★ 0same name
- lengrufeng2008/dsh-skill-library★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository42 other files it gives its agents.
- AGENTS.mdAGENTS.md
- addressing-findingsSkill
- auditSkill
- codebase-onboardingSkill
- codex-reviewSkill
- compatibilitySkill
- debuggingSkill
- developSkill
- distributed-racesSkill
- memorySkill
- nix-dev-envSkill
- nix-flakesSkill
- nixosSkill
- nix-packagingSkill
- nix-reviewSkill
- refactoringSkill
- retroSkill
- review-initSkill
- review-rulesSkill
- rust-architecture-reviewSkill
- rust-architectureSkill
- rust-cliSkill
- rust-cloud-nativeSkill
- rust-concurrencySkill
- rust-ecosystemSkill
- rust-embeddedSkill
- rust-errorsSkill
- rust-fintechSkill
- rust-idiomsSkill
- rust-macrosSkill
- rust-mlSkill
- rust-navigationSkill
- rust-performanceSkill
- rust-pluginsSkill
- rust-reviewSkill
- rust-securitySkill
- rust-testingSkill
- rust-traitsSkill
- rust-unsafeSkill
- rust-webSkill
- specsSkill
- sql-reviewSkill
The file
We split this skill into the rules it gives an agent
2 rules. 0 are common, 2 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
2
Distinctive
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 22
Every value has exactly one owner: When the owner goes out of scope, the value is dropped.
- Distinctive: line 47
Default to borrowing inputs.
“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Rust Ownership
The model, then the fixes. Ownership is one set of rules enforced at compile time — once the model is in your head, the errors become mechanical. Deep dives are in the sub-files.
When to Use
- The borrow checker is rejecting code (move / borrow / lifetime error)
- Designing a function signature: take
T,&T, or&mut T? - Choosing a container for sharing/mutation:
Box,Rc,RefCell,Cow - Returning a reference and hitting lifetime errors
- Tying cleanup to a scope (RAII/scope guard) or lazily initializing a value (
OnceLock/LazyLock)
The model in four rules
- Every value has exactly one owner. When the owner goes out of scope, the value is dropped.
- Assignment/passing moves non-
Copyvalues: the source is invalidated.Copytypes (integers,bool,char, shared refs, tuples/arrays ofCopy) are duplicated instead. - Borrowing:
&Tis a shared (read) reference,&mut Tis an exclusive (write) reference. - The borrow rule: at any time you may have either any number of
&Tor exactly one&mut T— never both. And a reference must never outlive the value it points to.
Everything the borrow checker says is one of these being violated. The fix is almost never
"add .clone()" — it's to satisfy the rule a different way.
Error → fix
| Error | Meaning | First thing to try | Where |
| E0382 | use of moved value | borrow (&x) instead of moving; or restructure so the move is last | borrowing.md |
| E0499 | two &mut to the same value | shorten one borrow's scope (NLL); split into disjoint fields | borrowing.md |
| E0502 | & and &mut at once | finish reading before mutating; copy the read value out first | borrowing.md |
| E0505 | move while borrowed | end the borrow before the move; clone if genuinely needed | borrowing.md |
| E0597 | borrowed value doesn't live long enough | extend the owner's scope; store the owner, not the borrow | lifetimes.md |
| E0515 | returns a reference to a local | return the owned value, or Cow | lifetimes.md |
| E0106 | missing lifetime specifier | annotate the relationship between input and output refs | lifetimes.md |
| E0507 | cannot move out of borrowed content | clone, std::mem::take/replace, or match by reference | borrowing.md |
Decision: own or borrow in a signature?
Default to borrowing inputs; own only when you must store or consume.
| Take | When |
&T | you only read it |
&mut T | you mutate it in place and the caller keeps it |
T | you store it, consume it, or need ownership (e.g. move into a thread/struct) |
impl AsRef<str> / &str | flexible read-only string input (accepts String and &str) |
impl Into<String> | you'll own a String but want callers to pass either |
Returning: prefer returning owned T. Return &T only when it clearly borrows from an input
(and let elision write the lifetime). For "borrowed usually, owned sometimes" → Cow
(lifetimes.md).
Decision: which pointer?
Quick guide; full treatment in pointers.md.
| Need | Use |
| Heap allocation / recursive type / trait object | Box<T> |
| Multiple owners, single thread | Rc<T> |
Mutate through a shared/& reference, single thread | RefCell<T> (or Cell<T> for Copy) |
| Shared and mutable, single thread | Rc<RefCell<T>> |
| Break a reference cycle | Weak<T> |
| Any sharing across threads | Arc / Mutex → see rust-concurrency |
Resource lifecycle: guards and lazy init
Once you own a resource, two questions follow: when does its cleanup run and when is it
created. RAII/scope guards tie cleanup to a scope (it can't be skipped by ?, early return, or
panic); OnceLock/LazyLock defer creation to first use. Full treatment — including the
let _ = footgun and the transaction-rollback "defuse" pattern — in lifecycle.md.
Boundary
- Anything crossing threads —
Arc<Mutex<T>>,Send/Sync, channels — isrust-concurrency. This skill is the single-threaded ownership model. - Lifetimes in trait bounds and generic design →
rust-traits; the mechanics of lifetimes live here.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

