rust-style

Installation
SKILL.md

Rust style

Use this skill when writing or reviewing general Rust code. The goal is clear, idiomatic code that fits its crate, not conformity to a detached style preference.

Start with the repository

Before suggesting a style change, inspect the relevant Cargo.toml files, rust-toolchain.toml, rustfmt.toml or .rustfmt.toml, Clippy configuration, CI commands, MSRV policy, and nearby code. Run the project's configured formatter and lints when requested. Preserve its edition, MSRV, public API, feature choices and established conventions.

Idioms that usually help

  • Accept borrowed inputs (&str, &[T], &Path) when the function only reads them. Take ownership when storing, transforming, or returning the value. Don't add lifetimes or Cow until they solve an observed API or allocation problem.
  • Make fallible operations return Result; represent expected absence with Option. Reserve panic!, expect and unwrap for tests, examples, or invariants whose violation truly means the program cannot continue. Give expect a useful invariant message.
  • Use ? to propagate errors when the caller can act on them. Add context or translate errors at a boundary where it improves diagnosis; don't erase useful source errors.
  • Prefer exhaustive match when cases need distinct behavior. Use if let or let ... else for a single case. Prefer iterators when they make transformations clearer, not as a rule against readable loops.
  • Use names that describe domain meaning. Follow Rust naming conventions (UpperCamelCase types/traits, snake_case functions/modules, SCREAMING_SNAKE_CASE constants) unless a generated API or external contract requires otherwise.
  • Keep comments focused on why a non-obvious choice exists. Use /// docs for public API behavior, constraints, errors and examples. Verify examples as doctests when practical.
  • Use #[derive] for semantics that are genuinely correct for the type. Do not derive Copy, equality, ordering, or serialization merely for convenience.

Errors and panics

Installs
1
First Seen
5 days ago