Skip to content
Back to skills

Rust Design Patterns

ASecurity

Resolve Rust ownership and lifetime problems or design Rust APIs, resource lifecycles, and FFI boundaries.

  • 63 stars
  • 0 votes
  • 0 copies
  • 14 views
  • Added September 9, 2026
designrustgoapi

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned September 22, 2026

npx -y skills add ahonn/dotfiles --skill rust-design-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Rust Design Patterns?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Rust Design Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ahonn-rust-design-patterns/badge)](https://www.skillsdirectory.com/skills/ahonn-rust-design-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: rust-design-patterns
description: Resolve Rust ownership and lifetime problems or design Rust APIs, resource lifecycles, and FFI boundaries.
user-invocable: false
---

# 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

- Check the crate's edition, MSRV, and whether it is a library or binary. Library crates carry compatibility constraints (`non_exhaustive`, lint policy, public types) that binaries do not.
- Follow the crate's existing conventions for errors, builders, and unsafe boundaries before introducing a new pattern.
- For borrow checker errors, reshape ownership before reaching for `clone`, `Rc`, or `Arc`. Treat a clone as a deliberate cost, not a way to silence the compiler.
- When a lifetime is too short and no borrow reshaping fits, prefer an owned type over widening lifetimes or adding `'static` bounds.

## Read by problem

| Problem or decision | Reference |
|---------------------|-----------|
| Move a value out of `&mut`, or swap enum variants | [ownership-patterns.md](references/ownership-patterns.md) — mem::take and mem::replace |
| Borrow two fields of a struct independently | [ownership-patterns.md](references/ownership-patterns.md) — Struct Decomposition |
| Choosing shared ownership, or tempted to clone | [ownership-patterns.md](references/ownership-patterns.md) — Rc/Arc Decision Guide |
| Guaranteed cleanup (locks, files, transactions) | [ownership-patterns.md](references/ownership-patterns.md) — RAII Guards, Finalisation in Destructors |
| Unsafe code needs a safe boundary | [ownership-patterns.md](references/ownership-patterns.md) — Contain Unsafety in Small Modules |
| Function parameter types (`&str` vs `&String`) | [api-design.md](references/api-design.md) — Borrowed Types for Arguments |
| Many optional constructor parameters | [api-design.md](references/api-design.md) — Builder Pattern, Default Trait |
| Distinct types over the same representation | [api-design.md](references/api-design.md) — Newtype Pattern |
| Compile-time state transitions | [api-design.md](references/api-design.md) — Type State Pattern |
| Public types that must evolve without breaking callers | [api-design.md](references/api-design.md) — non_exhaustive, Constructor Conventions |
| FFI errors, strings, or object lifetimes | [api-design.md](references/api-design.md) — FFI Patterns |
| Dynamic dispatch without boxing | [idioms.md](references/idioms.md) — On-Stack Dynamic Dispatch |
| Closure captures, post-setup immutability, `Option` in iterator chains | [idioms.md](references/idioms.md) |
| A consuming call fails and the caller needs the value back | [idioms.md](references/idioms.md) — Return Consumed Argument on Error |
| Reviewing existing Rust for common mistakes | [common-pitfalls.md](references/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.

Files in this skill

  • SKILL.md8 KB
  • references/api-design.md11.8 KB
  • references/common-pitfalls.md5.8 KB
  • references/ownership-patterns.md7.8 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in design →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.