agentleFS
KeWang0622KeWang0622/clonetax/skills/rust-ownership/SKILL.md

rust-ownership

Skill0 starsChanged 3 months agoMITView on GitHub

Stops coding agents from "fixing" Rust borrow-checker errors with .clone(), Arc, RefCell or Box::leak when the real fix is to restructure ownership. Use when writing or repairing Rust code, when rustc reports E0382, E0499, E0502, E0506, E0515 or E0597, when a borrow checker error must be resolved, or when reviewing Rust code for unnecessary clones and allocations.

5 other projects use a version of this.

Adopted by

Most recently taken up by

  • Other versions5 projects use a version of this.

    Word for word · 0

    None found.

    Edited · 0

    None found.

    Wrote their own version · 5

    1. huiali/rust-skills★ 47same name
    2. adxptived/Rust-Skills★ 1same name
    3. gurinderu/craft★ 1same name
    4. dawidpereira/rust-skills★ 0same name
    5. 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.

The file

We split this skill into the rules it gives an agent

6 rules. 0 are common, 4 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 4

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When rustc rejects your code with a borrow or move error, it usually gives you

    line 12
  2. Distinctive:

    When you hit a borrow error, work in this order:

    line 27
  3. Very specific:

    Change the signature: If a callee only reads, take &T / &str instead of T.

    line 31
  4. Distinctive:

    Sequence the accesses: Do the read, finish with it, then do the write.

    line 32
  5. Distinctive:

    Only then: consider owning the data — and when you do, say in one sentence why sharing or copying is the right design, not just what made …

    line 34
  6. Very specific:

    Every claim above comes from compiling the programs in corpus/ with a real rustc and reading --error-format=json.

    line 68

“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 — do not clone your way out

When rustc rejects your code with a borrow or move error, it usually gives you nothing to act on: on 5 of the 6 cases in this repo's corpus it emits no help and no note at all. In the one case where it does offer a help, that help is "consider cloning the value".

So the path of least resistance is to clone until it compiles. Do not take it. Cloning to escape a borrow conflict does not fix the conflict — it makes two objects where the code assumed one.

The rule

Never introduce .clone(), Arc, Rc, RefCell or Box::leak for the purpose of silencing a compiler error. Those are designs, not fixes. Reach for them only when the design genuinely calls for shared or owned data, and say so explicitly.

When you hit a borrow error, work in this order:

  1. Shorten the borrow. Bind only what you need (a bool, a usize, an owned key) inside a tight scope so the borrow ends before the conflicting use.
  2. Change the signature. If a callee only reads, take &T / &str instead of T.
  3. Sequence the accesses. Do the read, finish with it, then do the write.
  4. Use the API built for it. entry, split_at_mut, indices, std::mem::take.
  5. Only then consider owning the data — and when you do, say in one sentence why sharing or copying is the right design, not just what made the error go away.

Per-error playbook

CodeWhat it meansDo NOTDo
E0382Value used after being movedPass x.clone()Take &T in the callee
E0499Two mutable borrows at onceClone the collectionSequence them, or split_at_mut
E0502Mutable borrow while immutably borrowedClone the looked-up valueEnd the read in a tight scope, or use entry
E0506Assign to a borrowed valueClone the structShorten the reference's lifetime
E0515Return a reference to a localBox::leakReturn the owned value
E0597Borrow outlives its referentWrap in Rc<RefCell<_>>Restructure so the value outlives the borrow

The one that actually bites

E0499 and E0502 are the dangerous ones. Cloning there compiles cleanly, passes tests, and is silently wrong: the two halves of the code now mutate different objects, so writes through one are invisible to the other.

clippy::redundant_clone will not save you. It only fires when the original is never used again. In exactly these cases the original is still used — that is why the borrow conflicted — so clippy stays silent.

When cloning IS right

Cloning is not banned. It is correct when the data genuinely needs independent ownership: sending a value to another thread, storing a snapshot that must not change, or when profiling shows the copy is cheap and the restructure is not worth it. The test is whether you can state the reason without referring to the compiler error.

Evidence

Every claim above comes from compiling the programs in corpus/ with a real rustc and reading --error-format=json. See EVIDENCE.md, which is generated by node scripts/extract.mjs and re-verified in CI.

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.