Rust design patterns

From ahonn/dotfiles · View interactive version

Rust design patterns is an AI agent skill from the GitHub repository ahonn/dotfiles. Resolve Rust ownership and lifetime problems or design Rust APIs, resource lifecycles, and FFI boundaries.

How to use this skill

Skill file

Rust 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

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.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.