rust-borrow-checker
Skill0 starsChanged 3 days agoMITView on GitHub
Explain and fix Rust borrow-checker and lifetime errors step by step - E0382 use after move, E0499 and E0502 conflicting borrows, E0505 move while borrowed, E0507 move out of a reference, E0515 returning a reference to a local, E0597 and E0716 values that do not live long enough, E0373 closures that may outlive their environment, E0106 missing lifetimes. Includes a summariser for cargo --message-format=json output that merges duplicate diagnostics, groups by code and file, shows span labels and compiler suggestions and adds a fix hint per error code. Use when the user pastes a borrow or lifetime error, says "fighting the borrow checker", "cannot borrow as mutable", "does not live long enough", "value moved here", or has many compile errors to triage after a refactor.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- rainoftime/pl-skillstheir own version, first seen Feb 19, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- rainoftime/pl-skills★ 17same 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 repository9 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rust-async-reviewSkill
- rust-crates-publishSkill
- rust-dependency-auditSkill
- rust-error-handlingSkill
- rust-lint-ciSkill
- rust-testing-benchesSkill
- rust-unsafe-reviewSkill
- rust-workspace-setupSkill
The file
Files
Borrow checker explainer
Goal: understand which two uses conflict and why, then pick the smallest fix that
keeps the code idiomatic. Never "fix" a borrow error by adding unsafe, and do not
reach for clone(), Rc<RefCell<_>> or 'static until a structural fix is ruled out.
Paths below are relative to this skill's folder (in Claude Code: ${CLAUDE_SKILL_DIR}).
When to use
- A pasted error with
E0382,E0499,E0502,E0505,E0506,E0507,E0515,E0597,E0716,E0373,E0106, or "lifetime may not live long enough" - Dozens of errors after a refactor: triage them by code and file
- "Why does this need a lifetime?", "how do I return a reference from this function?"
When not to use
- Errors inside
unsafecode or about raw pointers:rust-unsafe-review future cannot be sent between threads safelyfrom a lock guard:rust-async-review- Error type design (
?conversions, thiserror):rust-error-handling
Workflow
- Get structured diagnostics (if you can run cargo; otherwise use the pasted text):
Fix errors in the order of the summary: the first error in a file often causes later ones.cargo check --all-targets --message-format=json > /tmp/diag.jsonl 2>/dev/null python3 scripts/diag_summary.py /tmp/diag.jsonl python3 scripts/diag_summary.py /tmp/diag.jsonl --code E0502 --json cargo clippy --all-targets --message-format=json | python3 scripts/diag_summary.py --level warning - Read the three spans the compiler gives: where the first borrow/move happens,
where the conflicting use is, and where the first borrow is used later. The labels
printed by the script are exactly these. State them to the user in one sentence:
"
vis borrowed immutably on line 4 (first), mutated on line 5, andfirstis still used on line 6." - Look up the pattern in references/errors.md (one section per
code, each with a minimal failing example and fixes in order of preference). For codes
not listed run
python3 scripts/diag_summary.py --explain E0xxxorrustc --explain E0xxx. - Choose the fix using this order:
- shorten the borrow (end it before the conflicting use; copy small values out)
- restructure (split borrows of different fields,
split_at_mut, iterate by index, two-phase "collect then mutate", entry API for maps) - change ownership (
&Tparameters instead ofT, return owned values,mem::take) - clone, if the value is small or the clone is not on a hot path
- shared ownership or interior mutability (
Rc/Arc,RefCell/Mutex) when the data really has several owners Explain why the chosen fix is safe; mention the cost of clones in hot loops.
- Apply and re-check:
cargo check --all-targetsand re-run the summary until clean. Show the diff; if a public signature changes, say so.
Hard cases
- Self-referential structs (a struct holding data and a reference into it): store
indices/ranges instead, or own the data elsewhere; crates like
ouroborosoryokeare a last resort. - Returning a reference to something created inside the function: impossible; return the owned value.
- Mutating a collection while iterating it: collect keys/indices first, use
retain,drain,iter_mut, or build a new collection. - Closures in threads/tasks (E0373):
moveplusArcfor shared data, orstd::thread::scopeto borrow from the stack. - NLL limitations (conditional return of a borrow from
gettheninsert, known as problem case 3): use the entry API or look up twice.
Output
3 errors (E0502 x1, E0382 x1, E0597 x1) in src/lib.rs, src/parse.rs
E0502 src/lib.rs:5 - `first` (shared borrow of v, line 4) is used on line 6 after v.push().
Fix: copy the value out: `let first = v[0];` (i32 is Copy). Diff below.
E0382 ...
After fixes: cargo check --all-targets is clean.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.

