Ownership, borrowing, and lifetime expert. Handles compiler errors E0382, E0597, E0506, E0507, E0515, E0716, E0106 and provides systematic solutions for memory safety patterns.
let s1 = String::from("hello");
let s2 = s1;
// println!("{}", s1); // Compile error!
Root Cause: Ownership transferred from s1 to s2, s1 is no longer valid.
Solutions:
clone()&s1s2 is temporary → consider redesignlet mut s = String::from("hello");
let r1 = &s;
let r2 = &mut s; // Conflict!
// println!("{}", r1);
Root Cause: Immutable and mutable borrows coexist.
Solutions:
fn longest<'a>(s1: &'a str, s2: &'a str) -> &'a str {
if s1.len() > s2.len() { s1 } else { s2 }
}
Root Cause: Return value lifetime must be tied to one of the inputs.
Key Solutions:
| Situation | Owner |
|-----------|-------|
| Function parameter | Caller owns |
| Function-local variable | Function owns (destroyed on return) |
| Struct field | Struct instance owns |
| Arc<T> | Multiple shared owners |
| Operation | Borrow Type | Notes |
|-----------|-------------|-------|
| Read-only | &T | Multiple can coexist |
| Needs mutation | &mut T | Only one at a time |
| Will original be modified during borrow? | If yes, that's the issue |
Return String instead of &str
↓
Use owned collections instead of slices
↓
Use Arc/Rc for shared ownership
↓
Lifetimes aren't always necessary
| Scenario | Choice | Reason |
|----------|--------|--------|
| Heap-allocate single value | Box<T> | Simple and direct |
| Single-threaded shared reference counting | Rc<T> | Lightweight |
| Multi-threaded shared reference counting | Arc<T> | Atomic operations |
| Need runtime borrow checking | RefCell<T> | Single-threaded interior mutability |
| Multi-threaded interior mutability | Mutex<T> or RwLock<T> | Thread-safe |
| Anti-Pattern | Problem | Correct Approach |
|--------------|---------|------------------|
| .clone() everywhere | Hides ownership issues | Think about actual ownership needs |
| 'static for everything | Too loose and imprecise | Use actual required lifetimes |
| Box::leak() memory leaks | Memory waste | Use proper lifetime management |
| Fighting the borrow checker | Digging your own hole | Understand and work with compiler design |
1. "When should I use references vs ownership?"
2. "How do I add lifetime annotations?"
'connection, 'file3. "Why doesn't this borrow work?"
| Code | Meaning | Don't Say | Ask Instead | |------|---------|-----------|-------------| | E0382 | Use of moved value | "clone it" | Who should own this data? | | E0597 | Lifetime too short | "extend lifetime" | Are scope boundaries correct? | | E0506 | Borrow not ended before mutation | "end borrow first" | Where should mutation occur? | | E0507 | Move out of reference | "clone before move" | Why move from reference? | | E0515 | Return non-owned data | "return owned" | Should caller own the data? | | E0716 | Temporary value lifetime insufficient | "bind to variable" | Why is this temporary? | | E0106 | Missing lifetime parameter | "add 'a" | What's the lifetime relationship? |
When encountering ownership issues, follow these steps:
1. What's this data's role in the domain?
2. Is ownership design intentional or accidental?
3. Fix symptom or redesign?
When ownership errors persist, trace to design level:
E0382 (moved value)
↑ Ask: What design choice led to this ownership pattern?
↑ Check: Is this an entity or value object?
↑ Check: Are there other constraints?
Persistent E0382 → rust-resource: Should use Arc/Rc for sharing?
Persistent E0597 → rust-type-driven: Are scope boundaries correct?
E0506/E0507 → rust-mutability: Should use interior mutability?
From design decisions to implementation:
"Data needs immutable sharing"
↓ Multi-threaded: Arc<T>
↓ Single-threaded: Rc<T>
"Data needs exclusive ownership"
↓ Return owned value
"Data is temporary use only"
↓ Use references within scope
"Need to pass data between functions"
↓ Consider lifetimes or return owned
When reviewing ownership-related code:
.clone() is used intentionally, not to avoid compiler errors# Check compilation
cargo check
# Run tests
cargo test
# Check for common mistakes
cargo clippy -- -W clippy::clone_on_copy -W clippy::unnecessary_clone
# Verify no memory leaks in tests
cargo test --features leak-check
Symptom: .clone() everywhere to satisfy compiler
Fix: Understand actual ownership requirements, use references where possible
Symptom: Complex lifetime annotations everywhere
Fix: Return owned types, use smart pointers
Symptom: Constantly rewriting to satisfy compiler
Fix: Step back and redesign data flow
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer