rust-best-practices

Reviews and guides Rust code using Apollo GraphQL's idiomatic best practices handbook.

7|1|Updated Mar 29, 2024
One-click install
npx skills add https://github.com/aBgAmeuR/harmony --skill rust-best-practices-abgameur
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-best-practices
Source: https://github.com/aBgAmeuR/harmony/tree/main/.agents/skills/rust-best-practices
Command: npx skills add https://github.com/aBgAmeuR/harmony --skill rust-best-practices-abgameur

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing idiomatic Rust requires mastering ownership, borrowing, error handling, linting, and testing conventions that are easy to get wrong. This Skill gives you a structured, chapter-by-chapter reference based on Apollo GraphQL's Rust Best Practices Handbook so you can write, review, and refactor Rust code against proven guidelines instead of guessing. ## Core Features & Use Cases - Code Review Guidance: Apply concrete rules for borrowing vs cloning, Option/Result handling, iterator usage, and comment discipline when reviewing Rust code. - Error Handling & Performance Patterns: Enforce Result-based error handling with thiserror/anyhow, avoid unwrap in production, and follow profiling-first performance practices with clippy and flamegraph. - Testing & Documentation Standards: Write descriptive tests, snapshot tests with cargo insta, and rustdoc documentation with executable examples. - Use Case: When refactoring a Rust service, ask the assistant to review a module; it will consult the relevant handbook chapters and flag redundant clones, missing error context, poor test naming, and missing doc comments with specific fixes. ## Quick Start Review my Rust file src/handler.rs using the rust-best-practices guidelines and suggest idiomatic improvements.

Frequently Asked Questions about rust-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write idiomatic Rust error handling?

Return Result<T, E> for fallible operations and avoid unwrap or expect in production code. Use thiserror for library error types with #[from] conversions, anyhow only for binaries, and the ? operator to propagate errors instead of match chains.

When should I clone versus borrow in Rust?

Prefer borrowing with &T over .clone() unless ownership transfer is required, such as caching results, sharing across threads with Arc, or API designs requiring owned data. Use &str over String and &[T] over Vec<T> in function parameters, and consider Cow for ambiguous ownership.

What clippy lints should I enable for Rust projects?

Run cargo clippy --all-targets --all-features --locked -- -D warnings regularly. Key lints include redundant_clone, large_enum_variant, needless_collect, and clone_on_copy. Configure workspace lints in Cargo.toml and use #[expect] with justification instead of #[allow].

Should I use generics or dyn Trait for polymorphism in Rust?

Prefer generics with static dispatch for performance-critical code since monomorphization has zero runtime cost. Use dyn Trait only when you need heterogeneous collections, plugin architectures, or runtime polymorphism, and box at API boundaries rather than internally.

How do I write good Rust tests?

Name tests descriptively like process_should_return_error_when_input_empty, keep one assertion per test, and organize related tests in modules. Use doc tests for public API examples and cargo insta for snapshot testing of generated output or serialized data.

When should I avoid the type state pattern in Rust?

Avoid type state for trivial states that enums handle fine, when runtime flexibility is required, or when it leads to overcomplicated generics and verbose duplication. Use it when compile-time state safety prevents real bugs, such as enforcing builder field completion or protocol ordering.