- Each value has exactly one owner - When the owner goes out of scope, the value is dropped
- You can have either one mutable reference OR any number of immutable references - Never both simultaneously
- References must always be valid - No dangling references, lifetimes must be sufficient
Understanding these rules is the key to fixing borrow checker errors.
- Diagnose a borrow checker error (paste the error)
- Fix a lifetime annotation issue
- Fix an ownership transfer problem
- Fix a mutable/immutable borrow conflict
- Understand a specific error code
Paste the compiler error or describe the issue, then I'll route to the appropriate workflow.
E0382 (use of moved value): Clone, use references, or restructure to avoid the move
E0502 (cannot borrow as mutable, already borrowed as immutable): Separate the borrows into different scopes, or use interior mutability (RefCell, Mutex)
E0499 (cannot borrow as mutable more than once): Split into separate scopes or use indices instead of references
E0597 (borrowed value does not live long enough): Extend the lifetime of the owner, or use owned types instead
E0506 (cannot assign, already borrowed): Complete borrow before assignment, or use Cell/RefCell
Core Rules:
- ownership-rules.md - The three ownership rules with examples
- borrowing-rules.md - Borrowing and reference rules
- lifetime-syntax.md - Lifetime annotation patterns and elision
Problem Solving:
- common-errors.md - Detailed breakdown of E0XXX errors
- patterns.md - Clone, Rc, Arc, RefCell, Cow patterns
1---2name: rust-borrow-checker3description: Debug Rust ownership, borrowing, and lifetime errors. Use when encountering borrow checker errors (E0382, E0502, E0597, etc.) or when code won't compile due to ownership issues.4---5
6<essential_principles>
7The Rust borrow checker enforces memory safety at compile time through three rules:
8
91. **Each value has exactly one owner** - When the owner goes out of scope, the value is dropped
102. **You can have either one mutable reference OR any number of immutable references** - Never both simultaneously
113. **References must always be valid** - No dangling references, lifetimes must be sufficient
12
13Understanding these rules is the key to fixing borrow checker errors.
14</essential_principles>
15
16<intake>
17What would you like help with?
18
191. Diagnose a borrow checker error (paste the error)
202. Fix a lifetime annotation issue
213. Fix an ownership transfer problem
224. Fix a mutable/immutable borrow conflict
235. Understand a specific error code
24
25**Paste the compiler error or describe the issue, then I'll route to the appropriate workflow.**
26</intake>
27
28<routing>
29| Response | Workflow |
30|----------|----------|
31| Error message pasted, "diagnose", "error", "E0XXX" | `workflows/diagnose-error.md` |
32| "lifetime", "'a", "outlives", "E0597", "E0621" | `workflows/fix-lifetime.md` |
33| "move", "moved", "ownership", "E0382", "E0507" | `workflows/fix-ownership.md` |
34| "borrow", "mutable", "immutable", "E0502", "E0499" | `workflows/fix-borrowing.md` |
35| Other | Clarify, then select appropriate workflow |
36</routing>
37
38<quick_fixes>
39Common solutions for frequent errors:
40
41**E0382 (use of moved value)**: Clone, use references, or restructure to avoid the move
42**E0502 (cannot borrow as mutable, already borrowed as immutable)**: Separate the borrows into different scopes, or use interior mutability (RefCell, Mutex)
43**E0499 (cannot borrow as mutable more than once)**: Split into separate scopes or use indices instead of references
44**E0597 (borrowed value does not live long enough)**: Extend the lifetime of the owner, or use owned types instead
45**E0506 (cannot assign, already borrowed)**: Complete borrow before assignment, or use Cell/RefCell
46</quick_fixes>
47
48<reference_index>
49Domain knowledge in `references/`:
50
51**Core Rules:**
52- ownership-rules.md - The three ownership rules with examples
53- borrowing-rules.md - Borrowing and reference rules
54- lifetime-syntax.md - Lifetime annotation patterns and elision
55
56**Problem Solving:**
57- common-errors.md - Detailed breakdown of E0XXX errors
58- patterns.md - Clone, Rc, Arc, RefCell, Cow patterns
59</reference_index>
60
61<workflows_index>
62| Workflow | Purpose |
63|----------|---------|
64| diagnose-error.md | Systematically diagnose any borrow checker error |
65| fix-lifetime.md | Resolve lifetime annotation issues |
66| fix-ownership.md | Fix ownership transfer problems |
67| fix-borrowing.md | Handle borrow conflicts |
68</workflows_index>
69
70<success_criteria>
71A fix is complete when:
72- `cargo check` passes without borrow checker errors
73- The solution doesn't introduce unnecessary cloning
74- The fix maintains the original intent of the code
75- Lifetimes are as simple as possible (rely on elision when possible)
76</success_criteria>