rust-best-practices

Applies idiomatic Rust guidelines for ownership, error handling, linting, testing, and performance.

Updated May 22, 2026
One-click install
npx skills add https://github.com/viniciuscs84/sdd-toolkit --skill rust-best-practices-viniciuscs84
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-best-practices
Source: https://github.com/viniciuscs84/sdd-toolkit/tree/main/skills/rust-best-practices
Command: npx skills add https://github.com/viniciuscs84/sdd-toolkit --skill rust-best-practices-viniciuscs84

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Rust code often compiles but still suffers from redundant clones, panicking error handling, missing lints, and untested edge cases. This Skill provides a structured set of idiomatic Rust guidelines so code reviews and refactors follow consistent ownership, error handling, performance, testing, and documentation practices. ## Core Features & Use Cases - Ownership and Borrowing Guidance: Rules for preferring &T over .clone(), &str over String, Copy types, and Cow for ambiguous ownership. - Error Handling Patterns: Result-based design with thiserror for libraries, anyhow for binaries, and ? propagation instead of unwrap/expect. - Linting and Performance Discipline: Clippy configuration, key lints like redundant_clone and large_enum_variant, plus benchmarking with --release and flamegraph profiling. - Testing and Documentation Standards: Descriptive test naming, doc tests, snapshot testing with cargo insta, and /// doc comment conventions. - Use Case: When refactoring a Rust service, apply the Skill to replace panic-prone unwrap() calls with typed thiserror errors, clean up redundant clones flagged by Clippy, and add doc tests to public APIs. ## Quick Start Review my Rust code using the rust-best-practices skill and suggest fixes for ownership, error handling, and Clippy warnings.

Frequently Asked Questions about rust-best-practices

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

FAQPage Schema
How do I handle errors idiomatically in Rust?

Return Result<T, E> for fallible operations and propagate errors with the ? operator instead of unwrap or expect. Use thiserror to define typed errors in libraries and anyhow only in binaries or main entry points where a catch-all with context is acceptable.

When should I use thiserror vs anyhow in Rust?

Use thiserror for library errors that callers need to match on, since it derives Display, Error, and From impls for precise typed errors. Reserve anyhow for binaries where ergonomic context chaining matters more than precise error types.

How do I avoid unnecessary cloning in Rust?

Prefer borrowing with &T over .clone(), use &str and &[T] in function parameters, and pass small Copy types by value. Run cargo clippy with the redundant_clone lint to detect accidental clones, and use Cow when ownership is ambiguous.

What Clippy lints should I enable for Rust projects?

Run cargo clippy --all-targets --all-features --locked -- -D warnings as a baseline. Key lints include redundant_clone, large_enum_variant, needless_collect, and clone_on_copy, configurable per workspace in Cargo.toml under [lints.clippy].

When should I use generics vs dyn Trait in Rust?

Prefer generics with static dispatch for performance-critical code since monomorphization produces inlined specialized code. Use dyn Trait only when you need heterogeneous collections, plugin architectures, or runtime polymorphism, accepting the vtable indirection cost.

Why should I avoid unwrap and expect in Rust production code?

unwrap and expect panic on failure, crashing the application instead of propagating recoverable errors. Alternatives include the ? operator, let-else patterns for early returns, and unwrap_or_else or unwrap_or_default for fallback values.