rust-design-patterns
Rust idioms and design patterns for writing idiomatic code. Use when: (1) Writing new Rust code, (2) Reviewing Rust code, (3) Solving borrow checker issues, (4) Designing Rust APIs. Triggers on: Rust
Files5
View on GitHubSKILL.md.claude/skills/rust-design-patterns/SKILL.md
GitHubRust Design Patterns
Start from the compiler error, API decision, or code under review. Read only the reference sections that match; the catalog is not a checklist to apply everywhere.
Establish applicability
- Check the crate's edition, MSRV, and whether it is a library or binary. Library crates carry compatibility constraints (
non_exhaustive, lint policy, public types) that binaries do not. - Follow the crate's existing conventions for errors, builders, and unsafe boundaries before introducing a new pattern.
- For borrow checker errors, reshape ownership before reaching for
clone,Rc, orArc. Treat a clone as a deliberate cost, not a way to silence the compiler. - When a lifetime is too short and no borrow reshaping fits, prefer an owned type over widening lifetimes or adding
'staticbounds.
Read by problem
| Problem or decision | Reference |
|---|---|
Move a value out of &mut, or swap enum variants |
ownership-patterns.md — mem::take and mem::replace |
| Borrow two fields of a struct independently | ownership-patterns.md — Struct Decomposition |
| Choosing shared ownership, or tempted to clone | ownership-patterns.md — Rc/Arc Decision Guide |
| Guaranteed cleanup (locks, files, transactions) | ownership-patterns.md — RAII Guards, Finalisation in Destructors |
| Unsafe code needs a safe boundary | ownership-patterns.md — Contain Unsafety in Small Modules |
Function parameter types (&str vs &String) |
api-design.md — Borrowed Types for Arguments |
| Many optional constructor parameters | api-design.md — Builder Pattern, Default Trait |
| Distinct types over the same representation | api-design.md — Newtype Pattern |
| Compile-time state transitions | api-design.md — Type State Pattern |
| Public types that must evolve without breaking callers | api-design.md — non_exhaustive, Constructor Conventions |
| FFI errors, strings, or object lifetimes | api-design.md — FFI Patterns |
| Dynamic dispatch without boxing | idioms.md — On-Stack Dynamic Dispatch |
Closure captures, post-setup immutability, Option in iterator chains |
idioms.md |
| A consuming call fails and the caller needs the value back | idioms.md — Return Consumed Argument on Error |
| Reviewing existing Rust for common mistakes | common-pitfalls.md — start with Review Checklist |
Examples illustrate the shape of a pattern, not code to paste. Verify version-sensitive behavior against the crate's toolchain.
Complete the change
Use the applicable AGENTS.md test mode. cargo clippy flags most of the unnecessary clones and borrowed-type issues these references describe.