rust-ownership-patterns

Diagnose and fix Rust ownership, borrowing, and lifetime compiler errors.

Updated Mar 29, 2026
One-click install
npx skills add https://github.com/luckyegg168/rust-skill --skill rust-ownership-patterns-luckyegg168
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: rust-ownership-patterns
Source: https://github.com/luckyegg168/rust-skill/tree/main/rust-skills/rust-ownership-patterns
Command: npx skills add https://github.com/luckyegg168/rust-skill --skill rust-ownership-patterns-luckyegg168

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Rust's borrow checker rejects code with errors like E0382, E0502, E0597, and E0106, and developers often struggle to choose between Move, Clone, Rc, Arc, Cow, and interior mutability patterns when designing data structures. ## Core Features & Use Cases - Compiler Error Repair: Maps common ownership errors (E0382 moved value, E0502 conflicting borrows, E0597 lifetime too short, E0106 missing lifetime specifier) to concrete fix strategies. - Ownership Strategy Selection: Decision tables for choosing between Copy, Clone, Move, &T, &mut T, Rc, Arc, Cow, Cell, RefCell, Mutex, and RwLock based on threading and sharing requirements. - Lifetime Annotation Guidance: Explains the three elision rules and when explicit <'a> annotations are required in functions, structs, and iterators. - Use Case: When the compiler rejects your code with "cannot borrow as mutable because it is also borrowed as immutable", consult this Skill to restructure borrows, narrow scopes, or apply RefCell for runtime-checked interior mutability. ## Quick Start Ask the AI to fix the borrow checker error in your Rust function and explain whether to use a reference, clone, or Rc for the shared value.

Frequently Asked Questions about rust-ownership-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix E0382 borrow of moved value in Rust?▼

E0382 occurs when a value is used after its ownership moved to another binding or function. Fix it by passing a reference with & instead of moving, calling .clone() to create an explicit deep copy, or restructuring the code so the original owner stays alive.

When should I use Rc vs Arc vs RefCell in Rust?▼

Use Rc for shared ownership in single-threaded code and Arc for shared ownership across threads. Use RefCell for interior mutability in single-threaded contexts, and Mutex or RwLock when mutation must be thread-safe.

What is the difference between Copy and Clone in Rust?▼

Copy is an implicit bitwise duplicate limited to stack-only types like i32, f64, and bool, so assignment never moves. Clone is an explicit .clone() call that can perform deep copies of heap data like String and Vec.

When are lifetime annotations required in Rust functions?▼

Explicit <'a> annotations are required when the compiler's three elision rules cannot determine which input reference the output borrows from, such as functions taking two or more references. Structs holding references must always declare lifetime parameters.

Why does Rust say cannot borrow as mutable more than once?▼

E0499 happens because Rust allows only one &mut reference at a time to prevent data races. Fix it by narrowing the scope of each mutable borrow, splitting borrows across disjoint fields, or using RefCell for runtime borrow checking.

When should I use Cow instead of String in Rust?▼

Use Cow<'_, str> when a function usually returns a borrowed slice but occasionally needs to allocate, such as normalization that only modifies some inputs. This avoids unnecessary heap allocations in the common borrowed case.