Rust Style Guide
Apply this checklist when writing or reviewing Rust code.
Trigger Reference
- •Use
references/trigger-matrix.mdas the canonical trigger and co-activation matrix. - •Resolve skill activation from changed files with
python3 scripts/resolve_style_guides.py <changed-path>...when automation is available. - •Validate trigger matrix consistency with
python3 scripts/validate_trigger_matrix_sync.py.
Architecture and module boundaries
Quality Gate Reference
- •Use
references/quality-gate-command-matrix.mdfor CI check-only vs local autofix command mapping.
- •Keep modules cohesive; expose only required public APIs.
- •Keep domain logic independent from transport/storage layers.
- •Isolate side effects and external dependencies behind traits where appropriate.
- •Avoid large monolithic modules; split by capability and ownership.
Naming and structure
- •Follow Rust naming conventions (
snake_case,CamelCase,SCREAMING_SNAKE_CASE). - •Keep functions focused and minimize deep nesting.
- •Replace magic numbers with named constants and units (
MAX_RETRIES,TIMEOUT_MS). - •Prefer explicitness over clever macro-heavy abstractions for core logic.
Type safety and data modeling
- •Model domain states with enums/newtypes to prevent invalid combinations.
- •Prefer typed structs over loosely typed maps for boundary payloads.
- •Encode invariants in constructors/builders.
- •Keep lifetimes and ownership semantics explicit where non-trivial.
Error handling
- •Return
Resultfor recoverable failures and use specific error enums/types. - •Add context to errors (
thiserror,anyhow::Context, or equivalent patterns). - •Avoid
unwrap/expectin production paths unless invariant is proven and documented. - •Handle errors intentionally at boundaries (retry/map/log/rethrow).
- •Do not hide root-cause failures behind generic fallback behavior.
Configuration and environment
- •Parse and validate configuration during startup.
- •Fail startup when required environment variables are missing.
- •Do not set fallback defaults for required environment variables.
- •Keep secrets out of source and logs.
Security and compliance
- •Validate all untrusted input and enforce schema constraints.
- •Use safe query APIs/parameterization for persistence layers.
- •Avoid invoking shell commands with unchecked user input.
- •Redact sensitive values in logs and error messages.
Performance and scalability
- •Profile before optimization and focus on measured bottlenecks.
- •Avoid unnecessary cloning/allocations in hot paths.
- •Stream large data and bound queues/channels.
- •Use explicit limits for retries, buffers, and parallelism.
Testing and verification
- •Add unit tests for pure logic and integration tests for external boundaries.
- •Cover edge cases: invalid inputs, timeout, cancellation, concurrent access.
- •Add regression tests for each bug fix.
- •Document manual verification for behavior not covered by automation.
Observability and operations
- •Emit structured logs with correlation IDs.
- •Emit metrics for latency, throughput, and failure classes.
- •Keep error types actionable for operational triage.
- •Ensure health checks surface dependency readiness.
CI required quality gates (check-only)
- •Run
cargo fmt --all -- --check. - •Run
cargo clippy --all-targets --all-features -- -D warnings. - •Run
cargo test --all-targets --all-features. - •Reject changes that weaken compile-time guarantees.
Optional autofix commands (local)
- •Run
cargo fmt --all.