rust-patterns

Enforce idiomatic Rust patterns for ownership, error handling, and concurrency.

Updated Sep 13, 2025
One-click install
npx skills add https://github.com/llmh333/employee_management_spring --skill rust-patterns-llmh333
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-patterns
Source: https://github.com/llmh333/employee_management_spring/tree/main/.gemini/skills/rust-patterns
Command: npx skills add https://github.com/llmh333/employee_management_spring --skill rust-patterns-llmh333

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill helps you avoid common Rust pitfalls by enforcing idiomatic patterns for ownership, error handling, modeling state with enums, and safe concurrency so your code becomes safer, clearer, and more maintainable.

Core Features & Use Cases

  • Ownership and borrowing guidance: Prefer borrowing over cloning, and use Cow to flexibly avoid unnecessary allocations.
  • Production-grade error handling: Use Result with ?, avoid unwrap(), and distinguish typed library errors with thiserror from flexible application errors with anyhow.
  • Correct modeling and control flow: Use enums and exhaustive pattern matching to make illegal states unrepresentable, avoiding wildcard matches for business-critical logic.
  • Type-safe abstractions: Apply generics, trait objects appropriately, and use the newtype pattern to prevent mixing primitive IDs.
  • Concurrency best practices: Use Arc<Mutex<T>> for shared mutable state and channels or async/Tokio patterns for message passing and timeouts.
  • Project structure and API visibility: Organize modules by domain and keep a minimal public surface with pub(crate) where appropriate.

Real-world example

When building a backend service that processes concurrent requests, this skill guides how to structure modules, propagate errors cleanly, model request state transitions with enums, and implement concurrency safely without data races.

Quick Start

Use the rust-patterns skill to review or refactor your Rust codebase for idiomatic ownership, safe error handling, exhaustive enum matching, and production-ready concurrency patterns.

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 and causing panics?

To handle errors in Rust safely, use the Result type with the ? operator to propagate failures instead of unwrap. For libraries, use thiserror for typed errors, and for applications, use anyhow for flexible error handling.

What's the best way to model state transitions in Rust to prevent invalid states?

The best way to model state in Rust is using enums with exhaustive pattern matching. This makes illegal states unrepresentable, ensuring your business logic handles every possible valid transition without relying on wildcard matches.

How do I manage ownership and borrowing to avoid unnecessary cloning in Rust?

To manage ownership efficiently in Rust, prefer borrowing over cloning and use Cow to flexibly avoid unnecessary allocations. This ensures memory safety while maintaining high performance across application and library code.

How do I implement safe concurrency in Rust for shared mutable state?

To implement safe concurrency in Rust, use Arc<Mutex<T>> for shared mutable state across threads. For message passing and timeouts, use channels or async/Tokio patterns to prevent data races and ensure safe concurrent execution.

When should I use thiserror vs anyhow for Rust error handling?

Use thiserror when building libraries to provide typed, exhaustive errors, and use anyhow for applications needing flexible error handling. This distinction ensures your Rust crate maintains a clean error propagation strategy appropriate for its context.

How do I structure Rust modules to keep a minimal public API surface?

To structure Rust modules effectively, organize them by domain and enforce minimal public visibility. Use pub(crate) where appropriate to hide internal implementation details, improving maintainability and safe-by-design correctness.