rust-ownership-borrowing
Design and review Rust ownership, borrowing, lifetimes, and interior mutability so data flow matches the domain model. Use when writing, refactoring, or reviewing Rust code that hits move, borrow checker, aliasing, Clone, Copy, Rc, Arc, RefCell, or lifetime problems.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 491
01 前言 Front Matter
- name
- rust-ownership-borrowing
- license
- MIT
- metadata
- { "tags": "rust,ownership,borrowing,lifetimes,borrow-checker", "version": "0.1.0", "category": "Rust", "displayName": "Rust Ownership Borrowing" }
- description
- Design and review Rust ownership, borrowing, lifetimes, and interior mutability so data flow matches the domain model. Use when writing, refactoring, or reviewing Rust code that hits move, borrow checker, aliasing, Clone, Copy, Rc, Arc, RefCell, or lifetime problems.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 2
- 参考文档agents/openai.yaml237 B
- 参考文档references/borrow-patterns.md1.9 KB
03 SKILL.md 正文
Rust Ownership Borrowing
Use this skill to make ownership shape match the program's data flow. Treat borrow-checker errors as design feedback: decide who owns each value, how long access must last, and whether mutation needs exclusivity or a different data shape.
Core Workflow
- Identify the owner of each value crossing the failing code path.
- Decide the required access for each function: own, shared borrow, mutable borrow, or shared ownership.
- Shorten borrows before changing types. Introduce blocks, temporary values, helper functions, or earlier extraction so references end before mutation.
- Prefer moving values when the caller no longer needs them. Prefer borrowing when the caller retains ownership. Clone only when duplicate ownership is intentional and the cost is acceptable.
- Replace self-referential or graph-like designs with IDs, indices, arenas, or
ownership trees before reaching for
Rc<RefCell<_>>. - Use
Rcfor single-threaded shared ownership andArcfor cross-thread shared ownership. AddCell,RefCell,Mutex, orRwLockonly when the mutation model is explicit. - Add tests that exercise the ownership-sensitive behavior, not just the compiler error that prompted the change.
Signature Rules
- Take
Twhen the function consumes or stores the value. - Take
&Twhen the function only reads during the call. - Take
&mut Twhen the function must mutate and no other access should occur. - Return owned values unless the returned reference is clearly tied to an input lifetime.
- Avoid accepting
&Vec<T>,&String, or&PathBufwhen&[T],&str, or&Pathexpresses the needed capability. - Avoid adding lifetime parameters to structs unless the struct truly borrows data owned elsewhere. Owned fields are usually simpler.
Common Fix Patterns
Read references/borrow-patterns.md when resolving non-trivial compiler
errors or reviewing a borrow-heavy patch.
- Narrow the scope of immutable borrows before taking a mutable borrow.
- Use
Option::take,std::mem::take, orstd::mem::replaceto move a field out while leaving a valid value behind. - Use collection APIs such as
split_at_mut,get_mut,entry,drain, andretaininstead of indexing patterns that create overlapping borrows. - Clone handles like
Arc,Rc, and cheap IDs freely when that is the intended ownership handle; avoid cloning payloads to silence the compiler. - Convert iterator chains to explicit loops only when it makes borrow lifetimes clearer or avoids hidden captures.
Review Checklist
- Ownership follows the domain model; types are not wrapped just to bypass the borrow checker.
Cloneis deliberate and tested for semantic independence where relevant.- Interior mutability has a clear invariant and a small access surface.
- Lifetimes describe real borrowing relationships, not guesses added until the compiler accepts the code.
- Async or threaded code uses
Arcplus the right synchronization primitive;RcandRefCellstay out of cross-thread paths.
04 同仓库更多 Skills
worktable-review-with-annotations
Create, inspect, reply to, update, or resolve situated Worktable annotations. Use when feedback belongs to a Doc section, block, text range, or HTML Doc, or when the user asks to address existing review feedback and coordinate a requested artifact edit.
worktable-find-and-synthesize-context
Find, assess, and synthesize context from Worktable Docs and Records. Use when the user asks what a workspace contains, wants workspace-backed research, needs current facts reconciled across artifacts, or requests a cited synthesis without changing workspace state.
worktable-create-or-update-docs
Create, edit, rename, or maintain narrative Worktable Docs. Use for plans, decisions, briefs, notes, research syntheses, and other durable prose when the user requests a new artifact or a safe update to an existing one.
worktable-create-or-manage-records
Organize repeated structured information as durable Worktable Records. Use for research datasets, inventories, directories, pipelines, trackers, and other collections whose items need identity, fields, filtering, links, validation, or individual updates.
worktable-create-or-manage-interactive-html
Create or improve durable visual and interactive artifacts in Worktable. Use for dashboards, trackers, calculators, mini apps, slide presentations, and other requests that benefit from a polished HTML Doc.
worktable-collaborate-threads
Read, answer, and hand off messages through durable Worktable threads with users or other authenticated participants. Use when the user asks to check addressed messages, reply to a participant, continue a thread, request another agent's help, wait briefly for a response, or watch for comments and messages sent to the agent.