design-patterns-rust

Applies GoF design patterns and idiomatic Rust alternatives when implementing or refactoring Rust code.

2|Updated May 16, 2026
One-click install
npx skills add https://github.com/avbel/ai-skills --skill design-patterns-rust-avbel
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: design-patterns-rust
Source: https://github.com/avbel/ai-skills/tree/main/skills/design-patterns-rust
Command: npx skills add https://github.com/avbel/ai-skills --skill design-patterns-rust-avbel

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Translating classic object-oriented design patterns into Rust is error-prone because ownership, enums, traits, and the lack of inheritance change how patterns like Builder, Singleton, Visitor, and Observer should be implemented. This Skill provides the idiomatic Rust mechanism for each pattern so you avoid fighting the borrow checker or faking inheritance. ## Core Features & Use Cases - Pattern Decision Guide: Maps every GoF pattern (Builder, Factory, Singleton, Adapter, Decorator, Composite, Strategy, Command, Observer, State, Visitor, and more) to its idiomatic Rust equivalent, with full code examples in reference files. - Rust-Specific Patterns: Covers Typestate with PhantomData, RAII/Drop guards, Newtype wrappers, and enum-based state machines that have no OOP counterpart. - Trait Objects vs Enum Dispatch: Provides a decision table comparing performance, memory, and extensibility so you choose the right dispatch mechanism. - Use Case: When refactoring a Rust service that uses clone() to satisfy the borrow checker, consult the anti-patterns section to restructure borrows and decompose structs instead. ## Quick Start Ask the agent to review your Rust module and suggest which design patterns or idioms from the Rust Design Patterns book should replace the current OOP-style structure.

Frequently Asked Questions about design-patterns-rust

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement the Builder pattern in Rust?▼

Use a consuming builder where each method takes self and returns Self, ending with a build(self) method that consumes the builder. This prevents reuse bugs via move semantics; a non-consuming &mut self variant allows reconfiguration but requires clone() in build().

Should I use trait objects or enums for polymorphism in Rust?▼

Use enums when all variants are known at compile time — they are roughly 10x faster with no vtable and contiguous memory. Use Box<dyn Trait> when third-party extensibility is needed, since new types can implement the trait anywhere.

How do I implement a singleton in Rust?▼

Use LazyLock for initialization on first access (Rust 1.80+) or OnceLock for explicit set-then-get semantics. Since a static requires Sync, wrap mutable state in Mutex or RwLock; prefer dependency injection where possible.

Can std::sync::mpsc be used for the Observer pattern in Rust?▼

Not directly for broadcast — mpsc is multi-producer, single-consumer, so each event reaches only one receiver. Keep one Sender per observer for fan-out, or use tokio::sync::broadcast or crossbeam-channel for true pub/sub.

Why is using clone() to satisfy the borrow checker an anti-pattern?▼

Cloning to appease the borrow checker hides underlying ownership problems and adds runtime cost. The fix is to restructure borrows, decompose structs into independent parts, or use mem::take and mem::replace to move values without cloning.

What is the typestate pattern in Rust?▼

Typestate encodes valid state transitions in the type system using PhantomData<State> marker types and per-state impl blocks. Invalid transitions become compile errors with zero runtime cost, since PhantomData is zero-sized.