Rust Quality
Core Question
Is this tested, linted, and organized for the next developer?
Quick Decisions
| Situation |
Action |
| New project |
Apply default Cargo.toml settings below |
| Adding a feature |
Write tests first — see rust-tests for unit/integration strategy |
| Reviewing code |
Check anti-patterns index |
| Setting up CI |
cargo fmt --check && cargo clippy -- -D warnings && cargo test |
| Organizing modules |
Feature-based, flat for small projects |
| Benchmarking |
Use criterion, never Instant::now() |
| Mocking dependencies |
Extract traits, use mockall |
| Property testing |
Use proptest for roundtrip/invariant checks |
| Workspace setup |
Inherit lints and deps from workspace root |
Default Cargo.toml Settings
Apply these settings to every new Rust project:
[package]
edition = "2024"
rust-version = "1.85"
[lints.clippy]
correctness = "deny"
suspicious = "warn"
style = "warn"
complexity = "warn"
perf = "warn"
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
panic = "abort"
strip = true
[profile.dev.package."*"]
opt-level = 3
For workspaces, define lints at the root and inherit:
# workspace Cargo.toml
[workspace.lints.clippy]
correctness = "deny"
suspicious = "warn"
style = "warn"
complexity = "warn"
perf = "warn"
# member Cargo.toml
[lints]
workspace = true
Clippy Lint Levels
Deny: correctness
Hard errors for code that is outright wrong. Catches infinite iterators, NaN comparisons,
impossible conditions, and invalid regex. Never allow these.
[lints.clippy]
correctness = "deny"
Warn: suspicious, style, complexity, perf
Soft warnings for likely bugs, non-idiomatic patterns, unnecessary complexity, and
performance anti-patterns.
[lints.clippy]
suspicious = "warn"
style = "warn"
complexity = "warn"
perf = "warn"
Selective: pedantic
Enable pedantic as a baseline, then disable noisy lints:
[lints.clippy]
pedantic = "warn"
missing_errors_doc = "allow"
missing_panics_doc = "allow"
module_name_repetitions = "allow"
must_use_candidate = "allow"
too_many_lines = "allow"
Additional recommended lints
[lints.clippy]
undocumented_unsafe_blocks = "warn"
[lints.rust]
missing_docs = "warn"
For published crates, add cargo = "warn" to catch missing metadata and wildcard
dependencies.
Project Structure Quick Reference
Small projects (< 10 files): flat
src/
├── main.rs
├── lib.rs
├── config.rs
├── database.rs
└── error.rs
Medium projects (10-20 files): feature-based modules
src/
├── main.rs # Thin entry point
├── lib.rs # Re-exports, module declarations
├── user/
│ ├── mod.rs
│ ├── model.rs
│ ├── repository.rs
│ └── service.rs
├── order/
│ ├── mod.rs
│ ├── model.rs
│ └── service.rs
└── shared/
├── mod.rs
├── error.rs
└── database.rs
Large projects: workspace
my-project/
├── Cargo.toml # [workspace] with shared lints/deps
├── crates/
│ ├── core/
│ ├── api/
│ └── cli/
└── tests/
Visibility rules
| Scope |
Keyword |
Use for |
| Public API |
pub |
Types and functions users need |
| Crate-internal |
pub(crate) |
Shared implementation details |
| Parent-only |
pub(super) |
Sibling submodule helpers |
| Private |
(default) |
Everything else |
Key patterns
- Keep
main.rs thin, logic in lib.rs for testability.
- Organize by feature (user/, order/), not by type (models/, services/).
- Use
pub use re-exports in mod.rs to create clean public APIs.
- Create a
prelude module for commonly used types in libraries.
- Put multiple binaries in
src/bin/.
- Use
mod.rs for complex modules, adjacent files for simple ones.
Usage Scenarios
Scenario 1: Setting up a new Rust project
- Apply default Cargo.toml settings (edition, lints, profiles).
- Create
src/lib.rs with module declarations and src/main.rs as thin entry point.
- Add
rustfmt.toml with edition = "2024" and max_width = 100.
- Set up CI:
cargo fmt --check && cargo clippy -- -D warnings && cargo test.
- Structure tests:
#[cfg(test)] mod tests in each file, tests/ for integration.
Scenario 2: Reviewing Rust code for quality
- Check the anti-patterns index for common issues.
- Verify all public items have documentation.
- Confirm tests follow arrange/act/assert with descriptive names.
- Look for
.unwrap() in non-test code (use ? or .expect() with context).
- Ensure dependencies are behind traits for testability.
- Run
cargo clippy -- -D warnings and fix all warnings.
Scenario 3: Adding tests to existing code
- Decide test boundaries: see rust-tests → Quick Decisions for unit vs integration.
- Property tests:
proptest! for roundtrip, idempotence, and invariant properties.
- Mocking: extract dependencies into traits, use
mockall for mock generation.
- Benchmarks:
criterion in benches/, with black_box to prevent optimization.
- Async tests:
#[tokio::test] for async functions.
Reference Index
| Reference |
Covers |
| references/testing.md |
Proptest, mockall, criterion, tokio::test, RAII fixtures, doctests. For unit/integration test strategy and organization, see rust-tests |
| references/linting.md |
Clippy lint levels, pedantic config, workspace lints, missing_docs, unsafe docs, cargo fmt in CI |
| references/project.md |
lib/main split, feature modules, visibility, re-exports, prelude, workspaces, dependency inheritance |
| references/anti-patterns.md |
15 common anti-patterns with bad/good examples and "when acceptable" guidance |
Cross-References
- Error handling patterns: see the
rust-errors skill for thiserror, anyhow,
Result<T, E>, and error context chains.
- API design and naming conventions: see the
rust-api skill for builder pattern, newtype,
From/Into, sealed traits, #[non_exhaustive], and naming (references/naming.md).
- Unit & integration testing strategy: see the
rust-tests skill for test boundaries,
module organization, test builders, error path testing, and integration test isolation.
- Performance optimization: see the
rust-perf skill for profiling, memory layout,
SIMD, and release profile tuning.
- Async patterns: see the
rust-async skill for tokio runtime, channels, cancellation,
and structured concurrency.
- Logging and observability: see the
rust-tracing skill for tracing setup, structured
logging, #[instrument], RUST_LOG, and OpenTelemetry integration.
1---2name: rust-quality3description: Rust code quality — linting, project structure, anti-patterns, and advanced testing tools. Use when configuring clippy lints, organizing modules and workspaces, reviewing code for common Rust anti-patterns like excessive cloning or unwrap abuse, or setting up proptest, mockall, or criterion. Also use when setting up a new Rust project's Cargo.toml with recommended lint and profile settings. For unit and integration test strategy, see rust-tests.4---56# Rust Quality78## Core Question910> Is this tested, linted, and organized for the next developer?1112## Quick Decisions1314| Situation | Action |15|-----------|--------|16| New project | Apply default Cargo.toml settings below |17| Adding a feature | Write tests first — see rust-tests for unit/integration strategy |18| Reviewing code | Check anti-patterns index |19| Setting up CI | `cargo fmt --check && cargo clippy -- -D warnings && cargo test` |20| Organizing modules | Feature-based, flat for small projects |21| Benchmarking | Use criterion, never `Instant::now()` |22| Mocking dependencies | Extract traits, use mockall |23| Property testing | Use proptest for roundtrip/invariant checks |24| Workspace setup | Inherit lints and deps from workspace root |2526## Default Cargo.toml Settings2728Apply these settings to every new Rust project:2930```toml31[package]32edition = "2024"33rust-version = "1.85"3435[lints.clippy]36correctness = "deny"37suspicious = "warn"38style = "warn"39complexity = "warn"40perf = "warn"4142[profile.release]43opt-level = 344lto = "fat"45codegen-units = 146panic = "abort"47strip = true4849[profile.dev.package."*"]50opt-level = 351```5253For workspaces, define lints at the root and inherit:5455```toml56# workspace Cargo.toml57[workspace.lints.clippy]58correctness = "deny"59suspicious = "warn"60style = "warn"61complexity = "warn"62perf = "warn"6364# member Cargo.toml65[lints]66workspace = true67```6869## Clippy Lint Levels7071### Deny: correctness7273Hard errors for code that is outright wrong. Catches infinite iterators, NaN comparisons,74impossible conditions, and invalid regex. Never allow these.7576```toml77[lints.clippy]78correctness = "deny"79```8081### Warn: suspicious, style, complexity, perf8283Soft warnings for likely bugs, non-idiomatic patterns, unnecessary complexity, and84performance anti-patterns.8586```toml87[lints.clippy]88suspicious = "warn"89style = "warn"90complexity = "warn"91perf = "warn"92```9394### Selective: pedantic9596Enable pedantic as a baseline, then disable noisy lints:9798```toml99[lints.clippy]100pedantic = "warn"101missing_errors_doc = "allow"102missing_panics_doc = "allow"103module_name_repetitions = "allow"104must_use_candidate = "allow"105too_many_lines = "allow"106```107108### Additional recommended lints109110```toml111[lints.clippy]112undocumented_unsafe_blocks = "warn"113114[lints.rust]115missing_docs = "warn"116```117118For published crates, add `cargo = "warn"` to catch missing metadata and wildcard119dependencies.120121## Project Structure Quick Reference122123### Small projects (< 10 files): flat124125```126src/127├── main.rs128├── lib.rs129├── config.rs130├── database.rs131└── error.rs132```133134### Medium projects (10-20 files): feature-based modules135136```137src/138├── main.rs # Thin entry point139├── lib.rs # Re-exports, module declarations140├── user/141│ ├── mod.rs142│ ├── model.rs143│ ├── repository.rs144│ └── service.rs145├── order/146│ ├── mod.rs147│ ├── model.rs148│ └── service.rs149└── shared/150 ├── mod.rs151 ├── error.rs152 └── database.rs153```154155### Large projects: workspace156157```158my-project/159├── Cargo.toml # [workspace] with shared lints/deps160├── crates/161│ ├── core/162│ ├── api/163│ └── cli/164└── tests/165```166167### Visibility rules168169| Scope | Keyword | Use for |170|-------|---------|---------|171| Public API | `pub` | Types and functions users need |172| Crate-internal | `pub(crate)` | Shared implementation details |173| Parent-only | `pub(super)` | Sibling submodule helpers |174| Private | (default) | Everything else |175176### Key patterns177178- Keep `main.rs` thin, logic in `lib.rs` for testability.179- Organize by feature (user/, order/), not by type (models/, services/).180- Use `pub use` re-exports in `mod.rs` to create clean public APIs.181- Create a `prelude` module for commonly used types in libraries.182- Put multiple binaries in `src/bin/`.183- Use `mod.rs` for complex modules, adjacent files for simple ones.184185## Usage Scenarios186187### Scenario 1: Setting up a new Rust project1881891. Apply default Cargo.toml settings (edition, lints, profiles).1902. Create `src/lib.rs` with module declarations and `src/main.rs` as thin entry point.1913. Add `rustfmt.toml` with `edition = "2024"` and `max_width = 100`.1924. Set up CI: `cargo fmt --check && cargo clippy -- -D warnings && cargo test`.1935. Structure tests: `#[cfg(test)] mod tests` in each file, `tests/` for integration.194195### Scenario 2: Reviewing Rust code for quality1961971. Check the anti-patterns index for common issues.1982. Verify all public items have documentation.1993. Confirm tests follow arrange/act/assert with descriptive names.2004. Look for `.unwrap()` in non-test code (use `?` or `.expect()` with context).2015. Ensure dependencies are behind traits for testability.2026. Run `cargo clippy -- -D warnings` and fix all warnings.203204### Scenario 3: Adding tests to existing code2052061. Decide test boundaries: see rust-tests → Quick Decisions for unit vs integration.2072. Property tests: `proptest!` for roundtrip, idempotence, and invariant properties.2083. Mocking: extract dependencies into traits, use `mockall` for mock generation.2094. Benchmarks: `criterion` in `benches/`, with `black_box` to prevent optimization.2105. Async tests: `#[tokio::test]` for async functions.211212## Reference Index213214| Reference | Covers |215|-----------|--------|216| [references/testing.md](references/testing.md) | Proptest, mockall, criterion, tokio::test, RAII fixtures, doctests. For unit/integration test strategy and organization, see rust-tests |217| [references/linting.md](references/linting.md) | Clippy lint levels, pedantic config, workspace lints, missing_docs, unsafe docs, cargo fmt in CI |218| [references/project.md](references/project.md) | lib/main split, feature modules, visibility, re-exports, prelude, workspaces, dependency inheritance |219| [references/anti-patterns.md](references/anti-patterns.md) | 15 common anti-patterns with bad/good examples and "when acceptable" guidance |220221## Cross-References222223- **Error handling** patterns: see the `rust-errors` skill for `thiserror`, `anyhow`,224 `Result<T, E>`, and error context chains.225- **API design** and naming conventions: see the `rust-api` skill for builder pattern, newtype,226 `From`/`Into`, sealed traits, `#[non_exhaustive]`, and naming (references/naming.md).227- **Unit & integration testing** strategy: see the `rust-tests` skill for test boundaries,228 module organization, test builders, error path testing, and integration test isolation.229- **Performance** optimization: see the `rust-perf` skill for profiling, memory layout,230 SIMD, and release profile tuning.231- **Async patterns**: see the `rust-async` skill for tokio runtime, channels, cancellation,232 and structured concurrency.233- **Logging and observability**: see the `rust-tracing` skill for tracing setup, structured234 logging, `#[instrument]`, RUST_LOG, and OpenTelemetry integration.