rust-design-patterns

Provide idiomatic Rust design patterns and idioms for code review and API design.

62|2|Updated Feb 21, 2017
One-click install
npx skills add https://github.com/ahonn/dotfiles --skill rust-design-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-design-patterns
Source: https://github.com/ahonn/dotfiles/tree/main/.claude/skills/rust-design-patterns
Command: npx skills add https://github.com/ahonn/dotfiles --skill rust-design-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

## What problem does it solve? Rust design patterns and idioms help you compose safe, idiomatic Rust code that handles ownership, borrowing, and API design challenges.

## Core Features & Use Cases

  • Borrowed Types (CRITICAL) patterns for flexible function inputs and efficient interfaces.
  • mem::take, mem::replace, and struct decomposition to move data without clones.
  • Builder and Newtype patterns for robust configuration and type safety.
  • Default trait and struct update syntax for ergonomic defaults.
  • Type State pattern and RAII guards for safe state machines and resource management.
  • FFI patterns for safe interop with C and other languages.
  • Anti-pattern awareness to avoid common mistakes such as over-cloning and Deref misuse.

### Quick Start Provide the most idiomatic Rust design-pattern recommendation for a given scenario and show a minimal example.

Frequently Asked Questions about rust-design-patterns

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

FAQPage Schema
How do I solve borrow-checker issues when writing Rust code?

Solve borrow-checker issues by applying Rust idioms like Borrowed Types for flexible inputs and mem::take to move data without clones. These design patterns restructure ownership and borrowing to satisfy the compiler while maintaining zero-cost abstractions.

What is the best way to design a flexible Rust API for struct configuration?

The best way to configure Rust structs is using the Builder pattern combined with the Default trait. This design approach provides ergonomic defaults and robust type safety, allowing callers to specify only the fields they need.

How does the Newtype pattern work in Rust and when should I use it?

The Newtype pattern in Rust works by wrapping an existing type in a struct to enforce type safety and distinguish different domain values. Use this design pattern to prevent logical errors and encapsulate validation logic at the type level.

How do I move data out of a borrowed reference in Rust without cloning?

Move data out of a borrowed reference without cloning by applying the mem::take or mem::replace design patterns. These Rust idioms allow struct decomposition and data transfer by swapping values with a default placeholder.

Are there specific FFI patterns for safe interop with C in Rust?

Yes, specific FFI patterns exist for safe interop with C in Rust. These design patterns manage resource boundaries and ownership across the foreign function interface, ensuring safe memory management when interacting with external C libraries.

What are common Rust anti-patterns I should avoid during code review?

Common Rust anti-patterns to avoid during code review include over-cloning data unnecessarily and misusing the Deref trait for inheritance. Avoiding these design mistakes ensures idiomatic code and proper ownership management.