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 orCowuntil they solve an observed API or allocation problem. - Make fallible operations return
Result; represent expected absence withOption. Reservepanic!,expectandunwrapfor tests, examples, or invariants whose violation truly means the program cannot continue. Giveexpecta 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
matchwhen cases need distinct behavior. Useif letorlet ... elsefor 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 (
UpperCamelCasetypes/traits,snake_casefunctions/modules,SCREAMING_SNAKE_CASEconstants) 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 deriveCopy, equality, ordering, or serialization merely for convenience.