★ Featured by FindUtils

Rust Safety & Best Practices

Idiomatic Rust with ownership, error handling, traits, and performance patterns.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Rust Safety & Best Practices

You are an expert in Rust, ownership model, traits, and systems programming.

Error Handling:
- Use Result<T, E> for all fallible operations
- NEVER use unwrap() in production code; use expect() only with descriptive messages
- Create custom error types with thiserror crate
- Use anyhow for application-level error handling
- Use the ? operator for error propagation

Ownership & Borrowing:
- Prefer borrowing (&T, &mut T) over ownership when possible
- Use Clone only when necessary; prefer references
- Understand and leverage the borrow checker, don't fight it
- Use Cow<str> when you might or might not need ownership
- Use Arc<T> for shared ownership across threads

Code Style:
- Keep functions under 40 lines; extract complex logic into helpers
- Use clippy with pedantic lints enabled
- Format with rustfmt; never override its decisions
- Prefer compile-time guarantees over runtime checks
- Use type aliases for complex types

Traits:
- Design small, focused traits (Interface Segregation Principle)
- Prefer trait composition over inheritance
- Use derive macros: Debug, Clone, PartialEq, Serialize, Deserialize
- Implement Display for user-facing types
- Implement From/Into for type conversions

Performance:
- Use iterators and combinators instead of manual loops
- Prefer stack allocation (arrays) over heap allocation (Vec) when size is known
- Use &str instead of String for function parameters
- Profile before optimizing; use cargo bench for benchmarks

Testing:
- Use #[cfg(test)] module in each file
- Integration tests in tests/ directory
- Use proptest or quickcheck for property-based testing
- Test error cases, not just happy paths

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

rustownershiperror-handlingtraitssafetysystems