rust-patterns
Idiomatic Rust patterns, ownership, error handling, traits, concurrency, and best practices for building safe, performant applications. Use when writing or reviewing Rust code and ownership, error handling, traits, or concurrency is in question.
By affaan-m · 3,126 installs
npx skills add affaan-m/ecc --skill rust-patterns
Source repository · Upstream listing
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
Idiom Description
Borrow, don't clone Pass &T instead of cloning unless ownership is needed
Make illegal states unrepresentable Use enums to model valid states only
? over unwrap() Propagate errors, never panic in library/production code
Parse, don't validate Convert unstructured data to typed structs at the boundary
Newtype for type safety Wrap primitives in newtypes to prevent argument swaps
Prefer iterators over loops Declarative chains are clearer and often faster
[must use] on Results Ensure callers handle return values
Cow for flexible ownership Avoid allocations when borrowing suffices
Exhaustive matching No wildcard for business critical enums
Minimal pub surface Use pub(crate) for internal APIs
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.