rust-patterns

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

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Rust that merely compiles is not enough—code that overuses unwrap(), clones to silence the borrow checker, or blocks async executors leads to panics, wasted allocations, and subtle bugs in production. This Skill provides a concrete rulebook of idiomatic Rust patterns so code is safe, performant, and maintainable from the start. ## Core Features & Use Cases - Ownership & Error Handling: Enforces borrowing over cloning, Result/? propagation, thiserror for libraries, and anyhow for applications. - Type-Driven Design: Covers enums with exhaustive matching, the newtype pattern, builder pattern, and generics vs. trait objects to make illegal states unrepresentable. - Concurrency & Tooling: Guides safe shared state with Arc<Mutex<T>>, channels, Tokio async patterns, and essential commands like cargo clippy and cargo audit. - Use Case: When reviewing a pull request that calls .unwrap() in a config loader or uses std::thread::sleep inside an async function, apply this Skill to refactor it into ?-propagated errors with context and non-blocking tokio::time::sleep. ## Quick Start Review my Rust code 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 in Rust without using unwrap?▼

Propagate errors with the `?` operator and return `Result` types instead of calling `unwrap()`. Use `thiserror` to define typed errors in libraries and `anyhow` with `.context()` for flexible error handling in applications.

When should I use thiserror vs anyhow in Rust?▼

Use `thiserror` in library code where callers need structured, typed error enums they can match on. Use `anyhow` in application code where you mainly need to propagate errors with added context and bail on failures.

How do I share mutable state across threads in Rust?▼

Wrap the value in `Arc<Mutex<T>>` so multiple threads can own a reference and lock it for mutation. For message passing between threads, use `mpsc` channels, preferably bounded `sync_channel` for backpressure.

Why is std::thread::sleep bad inside async Rust code?▼

`std::thread::sleep` blocks the entire executor thread, stalling all tasks scheduled on it. In async contexts use `tokio::time::sleep(...).await`, which yields control so other tasks can run.

When is it acceptable to use unsafe code in Rust?▼

Unsafe is acceptable at FFI boundaries with documented invariants and in proven performance-critical paths, always with a `// SAFETY` comment explaining why the invariants hold. It is not acceptable for bypassing the borrow checker or convenience.

How should I organize modules in a Rust crate?▼

Organize modules by domain (e.g., `auth/`, `orders/`) rather than by type, and keep the public surface minimal. Use `pub(crate)` for internal sharing and re-export the public API from `lib.rs`.