Quick Reference
| Topic | File | Key Trap |
|---|
| Ownership & Borrowing | INLINECODE0 | 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
- - Variable moved after use — clone explicitly or borrow with INLINECODE11
for item in vec moves vec — use &vec or .iter() to borrowString moved into function — pass &str for read-only access
Borrowing — The Borrow Checker Always Wins
- - Can't have
&mut and & simultaneously — restructure or interior mutability - Returning reference to local fails — return owned value instead
- Mutable borrow through
&mut self blocks all access — split struct or INLINECODE20
Lifetimes — When Compiler Can't Infer
- -
'static means CAN live forever, not DOES — String is 'static capable - Struct with reference needs
<'a> — INLINECODE24 - Function returning ref must tie to input — INLINECODE25
Strings — UTF-8 Surprises
- -
s[0] doesn't compile — use .chars().nth(0) or INLINECODE28 .len() returns bytes, not chars — use INLINECODE30s1 + &s2 moves s1 — use format!("{}{}", s1, s2) to keep both
Error Handling — Production Code
- -
unwrap() panics — use ? or match in production ? needs Result/Option return type — main needs INLINECODE39expect("context") > unwrap() — shows why it panicked
Iterators — Lazy Evaluation
- -
.iter() borrows, .into_iter() moves — choose carefully .collect() needs type — collect::<Vec<_>>() or typed binding- Iterators are lazy — nothing runs until consumed
Concurrency — Thread Safety
- -
Rc is NOT Send — use Arc for threads Mutex lock returns guard — auto-unlocks on drop, don't hold across awaitRwLock deadlock — reader upgrading to writer blocks forever
Memory — Smart Pointers
- -
RefCell panics at runtime — if borrow rules violated Box for recursive types — compiler needs known size- Avoid
Rc<RefCell<T>> spaghetti — rethink ownership
Common Compiler Errors (NEW)
| Error | Cause | Fix |
|---|
| INLINECODE54 | Used after move | Clone or borrow |
| INLINECODE55 |
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 - Features are additive — can't disable a feature a dependency enables
[dev-dependencies] not in release binary — but in tests/examplescargo build --release much faster — debug builds are slow intentionally