agentleFS
gurinderugurinderu/craft/skills/rust-ownership/SKILL.md

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

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
  1. Distinctive:

    Every value has exactly one owner: When the owner goes out of scope, the value is dropped.

    line 22
  2. Distinctive:

    Default to borrowing inputs.

    line 47

“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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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

  1. Every value has exactly one owner. When the owner goes out of scope, the value is dropped.
  2. Assignment/passing moves non-Copy values: the source is invalidated. Copy types (integers, bool, char, shared refs, tuples/arrays of Copy) are duplicated instead.
  3. Borrowing: &T is a shared (read) reference, &mut T is an exclusive (write) reference.
  4. The borrow rule: at any time you may have either any number of &T or 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

ErrorMeaningFirst thing to tryWhere
E0382use of moved valueborrow (&x) instead of moving; or restructure so the move is lastborrowing.md
E0499two &mut to the same valueshorten one borrow's scope (NLL); split into disjoint fieldsborrowing.md
E0502& and &mut at oncefinish reading before mutating; copy the read value out firstborrowing.md
E0505move while borrowedend the borrow before the move; clone if genuinely neededborrowing.md
E0597borrowed value doesn't live long enoughextend the owner's scope; store the owner, not the borrowlifetimes.md
E0515returns a reference to a localreturn the owned value, or Cowlifetimes.md
E0106missing lifetime specifierannotate the relationship between input and output refslifetimes.md
E0507cannot move out of borrowed contentclone, std::mem::take/replace, or match by referenceborrowing.md

Decision: own or borrow in a signature?

Default to borrowing inputs; own only when you must store or consume.

TakeWhen
&Tyou only read it
&mut Tyou mutate it in place and the caller keeps it
Tyou store it, consume it, or need ownership (e.g. move into a thread/struct)
impl AsRef<str> / &strflexible 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.

NeedUse
Heap allocation / recursive type / trait objectBox<T>
Multiple owners, single threadRc<T>
Mutate through a shared/& reference, single threadRefCell<T> (or Cell<T> for Copy)
Shared and mutable, single threadRc<RefCell<T>>
Break a reference cycleWeak<T>
Any sharing across threadsArc / 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 — is rust-concurrency. This skill is the single-threaded ownership model.
  • Lifetimes in trait bounds and generic design → rust-traits; the mechanics of lifetimes live here.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.