Rust Development Patterns
Idiomatic Rust patterns and best practices for building safe, performant, and maintainable applications.
When to Use
- Writing new Rust code
- Reviewing Rust code
- Refactoring existing Rust code
- Designing crate structure and module layout
How It Works
This skill enforces idiomatic Rust conventions across six key areas: ownership and borrowing to prevent data races at compile time, Result/? error propagation with thiserror for libraries and anyhow for applications, enums and exhaustive pattern matching to make illegal states unrepresentable, traits and generics for zero-cost abstraction, safe concurrency via Arc<Mutex<T>>, channels, and async/await, and minimal pub surfaces organized by domain.
Core Principles
1. Ownership and Borrowing
Rust's ownership system prevents data races and memory bugs at compile time.
Use Cow for Flexible Ownership
Error Handling
Use Result and ? — Never unwrap() in Production
Library Errors with thiserror, Application Errors with anyhow
Option Combinators Over Nested Matching
Enums and Pattern Matching
Model States as Enums
Exhaustive Matching — No Catch-All for Business Logic
Traits and Generics
Accept Generics, Return Concrete Types
Trait Objects for Dynamic Dispatch
Newtype Pattern for Type Safety
Structs and Data Modeling
Builder Pattern for Complex Construction
Iterators and Closures
Prefer Iterator Chains Over Manual Loops
Use collect() with Type Annotation
Concurrency
Arc<Mutex<T>> for Shared Mutable State
Channels for Message Passing
Async with Tokio
Unsafe Code
When Unsafe Is Acceptable
When Unsafe Is NOT Acceptable
Module System and Crate Structure
Organize by Domain, Not by Type
Visibility — Expose Minimally
Tooling Integration
Essential Commands
Quick Reference: Rust Idioms
Anti-Patterns to Avoid
Remember: If it compiles, it's probably correct — but only if you avoid unwrap(), minimize unsafe, and let the type system work for you.


