Rust
by @ivangdavila
Write idiomatic Rust avoiding ownership pitfalls, lifetime confusion, and common borrow checker battles.
clawhub install rustπ About This Skill
name: Rust slug: rust version: 1.0.1 description: Write idiomatic Rust avoiding ownership pitfalls, lifetime confusion, and common borrow checker battles. metadata: {"clawdbot":{"emoji":"π¦","requires":{"bins":["rustc","cargo"]},"os":["linux","darwin","win32"]}}
Quick Reference
| Topic | File | Key Trap |
|-------|------|----------|
| Ownership & Borrowing | ownership-borrowing.md | Move semantics catch everyone |
| Strings & Types | types-strings.md | String vs &str, UTF-8 indexing |
| Errors & Iteration | errors-iteration.md | unwrap() in production, lazy iterators |
| Concurrency & Memory | concurrency-memory.md | Rc not Send, RefCell panics |
| Advanced Traps | advanced-traps.md | unsafe, macros, FFI, performance |
Critical Traps (High-Frequency Failures)
Ownership β #1 Source of Compiler Errors
&for item in vec moves vec β use &vec or .iter() to borrowString moved into function β pass &str for read-only accessBorrowing β The Borrow Checker Always Wins
&mut and & simultaneously β restructure or interior mutability&mut self blocks all access β split struct or RefCellLifetimes β When Compiler Can't Infer
'static means CAN live forever, not DOES β String is 'static capable<'a> β struct Foo<'a> { bar: &'a str }fn get<'a>(s: &'a str) -> &'a strStrings β UTF-8 Surprises
s[0] doesn't compile β use .chars().nth(0) or .bytes().len() returns bytes, not chars β use .chars().count()s1 + &s2 moves s1 β use format!("{}{}", s1, s2) to keep bothError Handling β Production Code
unwrap() panics β use ? or match in production? needs Result/Option return type β main needs -> Result<()>expect("context") > unwrap() β shows why it panickedIterators β Lazy Evaluation
.iter() borrows, .into_iter() moves β choose carefully.collect() needs type β collect::>() or typed bindingConcurrency β Thread Safety
Rc is NOT Send β use Arc for threadsMutex lock returns guard β auto-unlocks on drop, don't hold across awaitRwLock deadlock β reader upgrading to writer blocks foreverMemory β Smart Pointers
RefCell panics at runtime β if borrow rules violatedBox for recursive types β compiler needs known sizeRc> spaghetti β rethink ownershipCommon Compiler Errors (NEW)
| Error | Cause | Fix |
|-------|-------|-----|
| value moved here | Used after move | Clone or borrow |
| cannot borrow as mutable | Already borrowed | Restructure or RefCell |
| missing lifetime specifier | Ambiguous reference | Add <'a> |
| the trait bound X is not satisfied | Missing impl | Check trait bounds |
| type annotations needed | Can't infer | Turbofish or explicit type |
| cannot move out of borrowed content | Deref moves | Clone or pattern match |
Cargo Traps (NEW)
cargo update updates Cargo.lock, not Cargo.toml β manual version bump needed[dev-dependencies] not in release binary β but in tests/examplescargo build --release much faster β debug builds are slow intentionally