- Rust 2024 edition,
cargo fmt+clippy -D warnings, zero warnings policy. Result<T, E>with a derive-based error crate (e.g., thiserror) for library errors, a context-adding error crate (e.g., anyhow) for applications.?for propagation — never.unwrap()in library code.- Minimize
unsafe— every block needs// SAFETY:comment explaining invariants. - Prefer
&stroverStringin params,Cow<'_, str>for conditional ownership,Arcfor shared ownership. impl Traitin argument position for static dispatch,dyn Traitfor dynamic dispatch when heterogeneous collections needed.- Small, focused modules. Use
pub(crate)for internal visibility. Workspace inheritance for multi-crate repos. #[cfg(test)]for unit tests,tests/for integration, a coverage tool (e.g., cargo-llvm-cov) for coverage.- Benchmarking: a statistical benchmark harness (e.g., criterion) for microbenchmarks, profile with a flamegraph tool (e.g., cargo flamegraph).
- Async:
tokioruntime,'static + Send + Syncbounds,tokio::spawnfor concurrency. - Security:
cargo auditfor CVE scanning,cargo denyfor license and advisory policies. - Dependencies: pin versions, commit
Cargo.lock, prefer well-maintained crates. - Structured logging (spans and events, e.g., the
tracingcrate) — notprintln!. - API naming: follow
as_/to_/into_conventions for conversions,iter()/iter_mut()/into_iter()for iterators. Getters arefield()notget_field(). See Rust API Guidelines. - Eagerly implement common traits:
Clone,Debug,Default,Eq,PartialEq,Hash,Send,Sync. UseFrom/AsRef/AsMutfor conversions,FromIterator/Extendfor collections. - Type safety: newtypes for static distinctions, builder pattern for complex construction,
bitflagsover enums for flag sets. Avoidboolparams — use custom types or enums. - Constructors:
new()as static inherent methods. No out-parameters. Only smart pointers implementDeref/DerefMut. - API flexibility: minimize parameter assumptions via generics, make traits object-safe when trait objects may be useful. Let callers decide where to copy and place data.
- Rustdoc: all public items have doc examples using
?(notunwrap). Document errors, panics, and safety invariants. Hyperlink related items. - Future-proofing: seal traits to prevent downstream implementations, keep struct fields private, don't duplicate derived trait bounds on structs. See Rust Design Patterns.
- Anti-patterns:
unwrap(), unguardedunsafe, panics in libraries,Vec/HashMapacross FFI.
Rust Conventions
Rust code conventions covering edition 2024, cargo fmt/clippy, Result-based error handling, unsafe discipline, async with tokio, API-guideline naming, trait implementations, benchmarking, and security scanning. Load when writing or reviewing Rust code.
Rust Conventions by goldziher · 07aa30f
npx skillmds@latest add goldziher/rust-conventions File contents
---name: rust-conventionsdescription: Rust code conventions covering edition 2024, cargo fmt/clippy, Result-based error handling, unsafe discipline, async with tokio, API-guideline naming, trait implementations, benchmarking, and security scanning. Load when writing or reviewing Rust code.---- Rust 2024 edition, `cargo fmt` + `clippy -D warnings`, zero warnings policy.- `Result<T, E>` with a derive-based error crate (e.g., thiserror) for library errors, a context-adding error crate (e.g., anyhow) for applications. `?` for propagation — never `.unwrap()` in library code.- Minimize `unsafe` — every block needs `// SAFETY:` comment explaining invariants.- Prefer `&str` over `String` in params, `Cow<'_, str>` for conditional ownership, `Arc` for shared ownership.- `impl Trait` in argument position for static dispatch, `dyn Trait` for dynamic dispatch when heterogeneous collections needed.- Small, focused modules. Use `pub(crate)` for internal visibility. Workspace inheritance for multi-crate repos.- `#[cfg(test)]` for unit tests, `tests/` for integration, a coverage tool (e.g., cargo-llvm-cov) for coverage.- Benchmarking: a statistical benchmark harness (e.g., criterion) for microbenchmarks, profile with a flamegraph tool (e.g., cargo flamegraph).- Async: `tokio` runtime, `'static + Send + Sync` bounds, `tokio::spawn` for concurrency.- Security: `cargo audit` for CVE scanning, `cargo deny` for license and advisory policies.- Dependencies: pin versions, commit `Cargo.lock`, prefer well-maintained crates.- Structured logging (spans and events, e.g., the `tracing` crate) — not `println!`.- API naming: follow `as_`/`to_`/`into_` conventions for conversions, `iter()`/`iter_mut()`/`into_iter()` for iterators. Getters are `field()` not `get_field()`. See [Rust API Guidelines](https://rust-lang.github.io/api-guidelines).- Eagerly implement common traits: `Clone`, `Debug`, `Default`, `Eq`, `PartialEq`, `Hash`, `Send`, `Sync`. Use `From`/`AsRef`/`AsMut` for conversions, `FromIterator`/`Extend` for collections.- Type safety: newtypes for static distinctions, builder pattern for complex construction, `bitflags` over enums for flag sets. Avoid `bool` params — use custom types or enums.- Constructors: `new()` as static inherent methods. No out-parameters. Only smart pointers implement `Deref`/`DerefMut`.- API flexibility: minimize parameter assumptions via generics, make traits object-safe when trait objects may be useful. Let callers decide where to copy and place data.- Rustdoc: all public items have doc examples using `?` (not `unwrap`). Document errors, panics, and safety invariants. Hyperlink related items.- Future-proofing: seal traits to prevent downstream implementations, keep struct fields private, don't duplicate derived trait bounds on structs. See [Rust Design Patterns](https://rust-unofficial.github.io/patterns).- Anti-patterns: `unwrap()`, unguarded `unsafe`, panics in libraries, `Vec`/`HashMap` across FFI.
goldziher/ai-rulez/tree/main/internal/builtins/languages/rust/skills/rust-conventions commit 07aa30f153
Frequently asked questions
Run npx skillmds@latest add goldziher/rust-conventions in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Rust code conventions covering edition 2024, cargo fmt/clippy, Result-based error handling, unsafe discipline, async with tokio, API-guideline naming, trait implementations, benchmarking, and security scanning. Load when writing or reviewing Rust code. It is listed under Security on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
goldziher (@goldziher) published this skill. Their other Agent Skills are listed on their SkillMD profile.