🎁 Get the FREE AI Skills Starter Guide β€” Subscribe β†’
BytesAgainBytesAgain
πŸ¦€ ClawHub

Rust

by @ivangdavila

Write idiomatic Rust avoiding ownership pitfalls, lifetime confusion, and common borrow checker battles.

Versionv1.0.1
Downloads3,044
Stars⭐ 4
TERMINAL
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

  • Variable moved after use β€” clone explicitly or borrow with &
  • for item in vec moves vec β€” use &vec or .iter() to borrow
  • String 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 RefCell
  • Lifetimes β€” When Compiler Can't Infer

  • 'static means CAN live forever, not DOES β€” String is 'static capable
  • Struct with reference needs <'a> β€” struct Foo<'a> { bar: &'a str }
  • Function returning ref must tie to input β€” fn get<'a>(s: &'a str) -> &'a str
  • Strings β€” 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 both
  • Error Handling β€” Production Code

  • unwrap() panics β€” use ? or match in production
  • ? needs Result/Option return type β€” main needs -> Result<()>
  • expect("context") > unwrap() β€” shows why it panicked
  • Iterators β€” Lazy Evaluation

  • .iter() borrows, .into_iter() moves β€” choose carefully
  • .collect() needs type β€” collect::>() 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 await
  • RwLock 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> spaghetti β€” rethink ownership

  • Common 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
  • Features are additive β€” can't disable a feature a dependency enables
  • [dev-dependencies] not in release binary β€” but in tests/examples
  • cargo build --release much faster β€” debug builds are slow intentionally