Development Standards & Preferences#
Rust Code Quality Standards#
Memory Safety & Performance#
- Pure Rust: No
unsafecode except in vetted dependencies - Zero Warnings: All code must pass
cargo clippy -- -D warnings - Performance First: Prefer zero-copy operations, efficient algorithms, and memory mapping for large data
- RAII Patterns: Leverage Rust's ownership system for resource management
Code Organization#
- File Size: Keep modules under 500 lines; split larger files into focused modules
- Module Hierarchy: Use clear module boundaries with
mod.rsfiles for organization - Public APIs: All public functions and types need comprehensive rustdoc with examples
- Internal Documentation: Document complex algorithms and business logic inline
Error Handling Philosophy#
- Result Types: Use
Result<T, E>patterns consistently throughout codebase - No Panics: Avoid panics in library code; reserve for truly unrecoverable situations
- Contextual Errors: Provide descriptive error messages with sufficient context for debugging
- Error Libraries: Prefer
thiserrorfor custom error types,anyhowfor application errors
Development Workflow & Tooling#
Preferred Build System#
- Just: Use justfile recipes for all development tasks instead of raw cargo commands
- Cross-platform: Ensure all recipes work on Linux, macOS, and Windows
- Composable: Break complex tasks into smaller, reusable recipes
Standard Development Commands#
# Development cycle
just check # Fast syntax/type checking and linting
just build # Build project
just test # Run all tests with nextest
just lint-rust # Linting with strict warnings
just fmt # Format code (Rust + markdown)
# Quality assurance
just bench # Run benchmarks (use criterion)
just docs-build # Generate documentation (mdBook + rustdoc)
just coverage # Coverage measurement with llvm-cov (target: >85%)
just audit # Security audit with cargo-audit
just format-docs # Format markdown files with mdformat
Testing Philosophy#
- Test Coverage: >85% coverage required for all changes
- Test Types: Unit tests (in-module), integration tests (in
tests/), property tests withproptest - Performance Tests: Benchmark critical paths with
criterion - Documentation Tests: Ensure all code examples in docs compile and run, mdformat checks pass on markdown files including embedded code blocks
- Deterministic Testing: Use
instafor snapshot testing of CLI outputs
Code Formatting & Linting#
- Rustfmt: Use project-wide
rustfmt.tomlfor consistent formatting - Clippy: Enable all lints, treat warnings as errors in CI
- Markdown Formatting: Use
mdformatwith extensions for consistent markdown formatting - Pre-commit Hooks: Run formatting and basic lints before commits
- IDE Integration: Configure rust-analyzer for real-time feedback
Documentation Standards#
API Documentation#
- Rustdoc: Comprehensive documentation for all public APIs
- Examples: Include working code examples in doc comments
- Error Cases: Document when functions return errors and why
- Safety: Document any unsafe code or invariants clearly
Project Documentation#
- mdBook: Use mdBook for user-facing documentation and guides
- User Guide Accuracy: The
docs/srcuser guide must accurately reflect exactly how the tool works right now, not aspirational features - Architecture Docs: Maintain high-level architecture documentation
- Decision Records: Document significant technical decisions and trade-offs
Dependency Management#
Dependency Selection Criteria#
- Maintenance: Prefer actively maintained crates with recent updates
- Security: Regular security audits, minimal dependency trees
- Performance: Choose performance-oriented crates for critical paths
- Compatibility: Ensure cross-platform support when needed
Preferred Crates#
- Error Handling:
thiserrorfor libraries,anyhowfor applications - CLI:
clapwith derive macros for argument parsing - Serialization:
serdeecosystem for JSON/YAML/TOML - Testing:
criterionfor benchmarks,proptestfor property testing - Async:
tokioecosystem when async is needed
Performance & Optimization#
Performance Principles#
- Measure First: Profile before optimizing, use
cargo benchandperf - Memory Efficiency: Prefer stack allocation, use
Box/Arcjudiciously - Zero-Copy: Minimize allocations in hot paths
- Lazy Evaluation: Defer expensive computations until needed
Profiling & Benchmarking#
- Criterion: Standard benchmarking with statistical analysis
- Flamegraphs: Use
cargo flamegraphfor performance profiling - Memory Profiling: Use
valgrindorheaptrackfor memory analysis - Continuous Benchmarking: Track performance regressions in CI