rust-design-patterns

Implements idiomatic Rust design patterns including Builder, Typestate, RAII, and Strategy.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Rust developers often struggle to choose and correctly implement idiomatic design patterns, leading to runtime errors, type confusion, and non-idiomatic APIs that fight the borrow checker instead of leveraging compile-time guarantees. ## Core Features & Use Cases - Pattern Catalog: Covers 12 idiomatic patterns including Newtype, From/Into, Builder, Typestate Builder, RAII Guard, Interior Mutability, Strategy (trait object vs enum dispatch), Sealed Trait, Extension Trait, Observer, and Command. - Compile-Time Safety Guidance: Shows how to use PhantomData and zero-sized types to enforce API usage order at compile time, and how to seal traits against downstream implementation. - Error Troubleshooting Table: Maps common compiler and runtime errors (BorrowMutError, unused type parameter, trait object restrictions) to concrete fixes. - Use Case: When designing a new HTTP client API, use the Typestate Builder pattern to guarantee at compile time that callers set the URL and method before calling build(), eliminating runtime validation. ## Quick Start Ask the AI to implement a typestate builder for an HTTP request struct that enforces setting the URL and method before build() can be called.

Frequently Asked Questions about rust-design-patterns

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

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

Create a separate builder struct with consuming methods that take mut self and return Self for chaining, ending with a build() method that produces the final struct. For compile-time enforcement of required fields, use the Typestate pattern with PhantomData or the typed-builder crate.

When should I use trait objects vs enum dispatch in Rust?▼

Use trait objects (dyn Trait) when the set of implementations must be extensible by external code, such as plugin architectures. Use enum dispatch when variants are fixed and known, since it avoids heap allocation and allows compiler inlining for better performance.

What is the Typestate pattern in Rust?▼

The Typestate pattern uses generic zero-sized type parameters and PhantomData to encode object state in the type system. Methods are only available in certain states, so invalid call sequences like building before setting required fields fail at compile time.

Why does RefCell panic with BorrowMutError at runtime?▼

BorrowMutError occurs when borrow_mut() is called while an immutable borrow() is still active, since RefCell enforces borrowing rules at runtime instead of compile time. Fix it by narrowing borrow scopes, or switch to Cell for Copy types or Mutex for thread-safe sharing.

How do I prevent other crates from implementing my trait?▼

Use the Sealed Trait pattern: define a private Sealed trait in a non-public module and make your public trait require it as a supertrait. Since external crates cannot access the private trait, they cannot implement your public trait.

What is the difference between Cell, RefCell, and Mutex in Rust?▼

Cell provides interior mutability for Copy types with compile-time checks and no thread safety. RefCell allows runtime-checked borrows for single-threaded code. Mutex provides thread-safe locking for shared mutable access across threads, typically wrapped in Arc.