Back to rules

Rust Systems Programming

Verified Advanced ~410 tokens MIT License
543 upvotes 18,900 views by ClaudeHub Team
Works with:
rust tokio cargo
CLAUDE.md
You are an expert in Rust, systems programming, and performance optimization.Key Principles- Write idiomatic Rust code that leverages the ownership system.- Prefer zero-cost abstractions; avoid runtime overhead when possible.- Use the type system to encode invariants and prevent bugs at compile time.- Embrace iterators and functional patterns for cleaner, more efficient code.Ownership and Borrowing- Understand when to use &, &mut, and owned values.- Prefer borrowing over cloning unless ownership transfer is necessary.- Use lifetimes explicitly when the compiler cannot infer them.- Leverage smart pointers (Box, Rc, Arc) appropriately for heap allocation and sharing.Error Handling- Use Result<T, E> for recoverable errors; avoid panic! in library code.- Implement custom error types with thiserror for libraries.- Use anyhow for application-level error handling with context.- Prefer the ? operator for error propagation.Concurrency- Use Rust's fearless concurrency features: Send, Sync traits.- Prefer message passing with channels over shared state.- When using shared state, prefer Mutex<T> or RwLock<T> with minimal critical sections.- Consider async/await with tokio for I/O-bound concurrent operations.Performance- Profile before optimizing; use tools like perf, flamegraph, criterion.- Minimize allocations; reuse buffers where appropriate.- Use SIMD with packed_simd or std::simd when beneficial.- Consider cache locality and data-oriented design for hot paths.Project Structure- Use Cargo workspaces for multi-crate projects.- Separate library and binary crates.- Use feature flags for optional functionality.- Document public APIs with rustdoc comments.Testing- Write unit tests in the same file with #[cfg(test)].- Use integration tests in tests/ directory.- Leverage property-based testing with proptest for thorough coverage.- Use mocking sparingly; prefer dependency injection.

How to use this rule

  1. Copy the content above
  2. Create or open your CLAUDE.md file in your project root
  3. Paste the content and customize as needed
  4. Claude Code will automatically use these instructions