58
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill ahonn/dotfiles --skill rust-design-patterns- SKILL.md8.0 KB
Overview
This skill provides concise Rust idioms and design patterns for writing idiomatic, safe, and performant Rust code. It focuses on ownership, borrowing, lifetimes, API design, and common anti-patterns to help you avoid borrow checker workarounds and subtle correctness issues. Use it as a checklist and quick reference during development or code review.
How this skill works
The skill inspects the problem context (borrow errors, lifetime complaints, API questions, performance concerns) and recommends targeted patterns: borrowed types, mem::take/replace, struct decomposition, newtype, builder, RAII guards, and on-stack dynamic dispatch. It also flags anti-patterns like unnecessary cloning, &String/&Vec in signatures, manual drop calls, and overuse of Deref for pseudo-inheritance. Recommendations include short code examples and actionable next steps for refactoring.
When to use it
- Writing new Rust code and choosing idiomatic types and constructors
- Reviewing code that uses clones or struggles with the borrow checker
- Solving ownership or lifetime errors while refactoring enums/structs
- Designing public APIs that should accept flexible borrowed inputs
- Working across FFI or unsafe boundaries and needing clear wrappers
Best practices
- Prefer borrowed slice/string types (&str, &[T]) in public APIs for flexibility
- When moving out of &mut fields, prefer mem::take/replace instead of cloning
- Use newtypes to get compile-time distinctions without runtime cost
- Apply Builder + Default for many optional constructor parameters and validation
- Encapsulate unsafe code in small modules and expose a safe API surface
Example use cases
- Refactor a function that accepts &String to accept &str so it accepts literals and slices
- Resolve a borrow checker error by mem::take on an enum field before replacing the variant
- Introduce a Builder for assembling a struct with many optional fields and validation
- Wrap a raw FFI pointer in an opaque Rust type with explicit free and error codes
- Replace repeated clone calls with Rc/Arc or a refactor that returns consumed values on error
FAQ
Use mem::take to take ownership out of a &mut field without allocating; clone only if you truly need independent copies. If multiple owners are needed, consider Rc/Arc.
Why prefer &str over &String in APIs?
Using &str accepts &String, string literals, and slices via deref coercion, making your API more flexible without runtime cost.