# Expert Rust Skill

> Instructions, patterns, and best practices for writing idiomatic, high-performance, and safe Rust code. Use when this capability is needed.

- Skill: `tomevault-io/expert-rust-skill` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/expert-rust-skill`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/expert-rust-skill/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/expert-rust-skill

---


# Expert Rust Skill

This skill serves as a guide for writing production-grade Rust code, focusing on safety, concurrency, and idiomatic patterns.

## 1. Ownership & Memory Management
- **Borrowing**: Prefer borrowing (`&T`, `&mut T`) over taking ownership when data doesn't need to be consumed.
- **Cloning**: Be explicit with `.clone()`. Avoid it in hot paths unless necessary. Use `Arc<T>` for shared ownership across threads/tasks.
- **Lifetimes**: Explicitly strictly define lifetimes `'a` only when the compiler cannot infer them.

## 2. Error Handling
- **Result & Option**: Never use `.unwrap()` or `.expect()` in production code unless you have proof that it will never panic (and comment that proof).
- **Propagation**: Use the `?` operator to propagate errors.
- **Crates**:
  - Use `thiserror` for library error definitions (enum implementation).
  - Use `anyhow` for application-level error handling and context (`.context("...")`).

## 3. Concurrency & Async
- **Runtime**: Use `tokio` for async operations.
- **Send & Sync**: Ensure types passed across thread/task boundaries implement `Send` (and `Sync` if shared).
- **Blocking**: NEVER block the async runtime thread. Use `tokio::task::spawn_blocking` for CPU-intensive or synchronous I/O operations.
- **Synchronization**: Use `tokio::sync::Mutex` for async critical sections, but prefer message passing (`tokio::sync::mpsc`) via channels over shared state where possible.

## 4. Type System & Traits
- **Newtypes**: Use the "Newtype" pattern (`struct ID(String)`) to enforce type safety and avoid primitives obsession.
- **Traits**: Prefer defining behavior via Traits. Use `impl Trait` for return types to reduce signature complexity.
- **Generics**: Use Generics with trait bounds (`T: AsRef<Path>`) to increase API flexibility.

## 5. Idiomatic Rust
- **Clippy**: Always respect `cargo clippy`. Treat warnings as errors.
- **Formatting**: Run `cargo fmt` automatically.
- **Iterators**: Prefer functional iterator chains (`.map()`, `.filter()`, `.collect()`) over raw `for` loops for readability and potential compiler optimizations.
- **Pattern Matching**: Use `match` with exhaustive patterns. Use `if let` for single pattern handling.

## 6. Testing
- **Unit Tests**: Place unit tests in the same file `mod tests { ... }`.
- **Integration Tests**: Place integration tests in `tests/` directory.
- **Doc Tests**: Write examples in documentation comments (`///`) which serve as tests.

## 7. Performance
- **Allocations**: Minimize heap allocations. Use `&str` instead of `String` when possible.
- **Data Structures**: Choose the right container (e.g., `HashMap`, `BTreeMap`, `Vec`) based on access patterns.
- **Buffering**: Use `BufReader`/`BufWriter` for I/O operations to reduce syscalls.

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/basilgregory) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-11 -->

