rust-patterns

Enforces idiomatic Rust patterns for ownership, error handling, traits, concurrency, and module structure.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Rust developers often write code that compiles but violates idiomatic conventions, leading to unnecessary clones, panics from unwrap(), hidden enum variants, and blocking calls in async contexts. This Skill provides concrete good/bad code patterns so reviews and new code follow community-accepted Rust idioms. ## Core Features & Use Cases - Ownership and Error Handling: Enforces borrowing over cloning, Result/? propagation, thiserror for libraries, and anyhow for applications. - Type-Driven Design: Covers enums with exhaustive matching, newtype wrappers, builder patterns, and generics versus trait objects. - Concurrency and Tooling: Guides Arc<Mutex<T>>, channels, Tokio async patterns, and cargo commands like clippy, fmt, test, and audit. - Use Case: When reviewing a pull request that uses .unwrap() in a library function or std::thread::sleep inside an async handler, apply this Skill to rewrite the code with proper error propagation and non-blocking awaits. ## Quick Start Review this Rust module and refactor it to follow idiomatic patterns for error handling, ownership, and concurrency.

Frequently Asked Questions about rust-patterns

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

FAQPage Schema
How do I handle errors idiomatically in Rust?

Propagate errors with Result and the ? operator instead of unwrap(). Use thiserror to define typed error enums in libraries and anyhow with context() for flexible error handling in application code.

When should I use thiserror vs anyhow in Rust?

Use thiserror for library code where callers need structured, matchable error types. Use anyhow for application code where you need flexible error propagation with added context and do not expose error types to consumers.

How do I share mutable state across threads in Rust?

Wrap the value in Arc<Mutex<T>> and clone the Arc for each thread. For message-passing workflows, use mpsc channels, preferably bounded sync_channel to apply backpressure.

Why is std::thread::sleep a problem in async Rust code?

std::thread::sleep blocks the executor thread, stalling all tasks scheduled on it. Use tokio::time::sleep().await instead so the runtime can schedule other work during the wait.

What are common Rust anti-patterns to avoid?

Avoid unwrap() in production code, cloning just to satisfy the borrow checker, String parameters where &str suffices, Box<dyn Error> in libraries, wildcard matches on business enums, and unsafe without documented safety invariants.