Rust

Use when: write or review idiomatic Rust with sound ownership, error handling, and zero-cost abstractions.

kimtth 2466b30 1.2 KB Updated

File contents

Goal: safe, idiomatic Rust that the borrow checker and tests both accept.

Use for:

  • new crates, modules, or performance-sensitive code
  • reviewing ownership, lifetimes, and error handling
  • replacing unwrap-heavy or clone-heavy code

Workflow:

  1. Model data with enums and structs; make invalid states unrepresentable.
  2. Return Result with a typed error; reserve panics for bugs.
  3. Borrow by default; clone only with a reason.
  4. Use iterators and combinators over manual index loops.
  5. Encode invariants in the type system, not runtime checks.
  6. Verify with cargo test, clippy, and cargo fmt.

Idioms:

  • ? operator for error propagation
  • Option/Result combinators (map, and_then, ok_or)
  • thiserror for libraries, anyhow for applications
  • traits for shared behavior; generics with bounds
  • prefer &str/&[T] params over owned types

Rules:

  • avoid unwrap/expect outside tests and proven invariants
  • do not reach for unsafe without a documented justification
  • let the borrow checker guide design, not fight it
  • keep clippy clean; warnings are signal

kimtth/agent-skill-100-lines-or-less/tree/main/skills/rust commit 2466b302a1

Frequently asked questions

npx skillmds@latest add kimtth/rust