language-rustlisted
Install: claude install-skill lugassawan/swe-workbench
# Rust
## Ownership in one paragraph
Every value has one owner. Passing by value moves; borrow with `&` (shared, many) or `&mut` (exclusive, one). Shared and mutable references cannot coexist. Most "fights with the borrow checker" are a design smell — the data model mixed ownership and sharing.
## Borrowing rules of thumb
- `&T` when you only read.
- `&mut T` when you mutate in place.
- `T` (by value) when you consume — builders, transforming constructors.
- Return owned types unless the caller clearly benefits from a borrow tied to a parameter's lifetime.
## Lifetimes
- Start without annotations; add them when the compiler asks.
- `'static` does not mean "lives forever" — it means "no non-static references inside". Prefer owned types over `'static` juggling.
- If a struct needs many lifetimes, consider owned data or `Arc` instead.
## Error handling
- Return `Result<T, E>`; use `?` to propagate.
- **Library code:** custom enum with `thiserror`.
- **Application code:** `anyhow::Error` for ergonomics; add context with `.with_context(|| "doing X")`.
- Reserve `panic!` for invariant violations the caller cannot recover from.
```rust
#[derive(thiserror::Error, Debug)]
pub enum LoadError {
#[error("io: {0}")] Io(#[from] std::io::Error),
#[error("bad format: {0}")] Format(String),
}
```
## Traits
- Design around capability, not identity (`Read`, not `IsFile`).
- Default methods let traits evolve without breaking implementors.
- `impl Trait` in arguments → static dispat