/thrashr888-agent-kit:rust-best-practicesfrom thrashr888-agent-kit
Guides idiomatic, efficient, and maintainable Rust code including error handling with anyhow/thiserror, ownership patterns, and builder API design.
Ships in thrashr888-agent-kit. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Guidelines for writing idiomatic, efficient, and maintainable Rust code.
anyhowuse anyhow::{Context, Result, bail};
fn load_config(path: &Path) -> Result<Config> {
let contents = std::fs::read_to_string(path)
.context("Failed to read config file")?;
let config: Config = toml::from_str(&contents)
.context("Failed to parse config")?;
if config.port == 0 {
bail!("Port cannot be 0");
}
Ok(config)
}
thiserroruse thiserror::Error;
#[derive(Error, Debug)]
pub enum ConfigError {
#[error("Failed to read config: {0}")]
Io(#[from] std::io::Error),
#[error("Failed to parse config: {0}")]
Parse(#[from] toml::de::Error),
#[error("Invalid configuration: {message}")]
Invalid { message: String },
}
.unwrap() in Library Code// BAD
let value = map.get("key").unwrap();
// GOOD
let value = map.get("key").ok_or_else(|| Error::MissingKey("key"))?;
// GOOD (when None is truly impossible)
let value = map.get("key").expect("key always present after init");
// BAD - unnecessary clone
fn process(data: String) { ... }
process(my_string.clone());
// GOOD - borrow when possible
fn process(data: &str) { ... }
process(&my_string);
Cow for Flexible Ownershipuse std::borrow::Cow;
fn process(data: Cow<'_, str>) -> Cow<'_, str> {
if data.contains("bad") {
Cow::Owned(data.replace("bad", "good"))
} else {
data // No allocation if unchanged
}
}
// GOOD - clear ownership
impl User {
pub fn new(name: impl Into<String>) -> Self {
Self { name: name.into() }
}
}
#[derive(Default)]
pub struct ServerBuilder {
host: Option<String>,
port: Option<u16>,
timeout: Option<Duration>,
}
impl ServerBuilder {
pub fn host(mut self, host: impl Into<String>) -> Self {
self.host = Some(host.into());
self
}
pub fn port(mut self, port: u16) -> Self {
self.port = Some(port);
self
}
pub fn build(self) -> Result<Server, ConfigError> {
Ok(Server {
host: self.host.unwrap_or_else(|| "localhost".into()),
port: self.port.ok_or(ConfigError::MissingPort)?,
timeout: self.timeout.unwrap_or(Duration::from_secs(30)),
})
}
}
// BAD - easy to mix up
fn transfer(from: i64, to: i64, amount: i64) { ... }
// GOOD - compile-time safety
pub struct AccountId(i64);
pub struct Amount(i64);
fn transfer(from: AccountId, to: AccountId, amount: Amount) { ... }
#[must_use] for Important Returns#[must_use]
pub fn validate(&self) -> Result<(), ValidationError> {
// ...
}
// BAD
let mut results = Vec::new();
for item in items {
if item.is_valid() {
results.push(item.transform());
}
}
// GOOD
let results: Vec<_> = items
.into_iter()
.filter(|item| item.is_valid())
.map(|item| item.transform())
.collect();
collect() Type Inference// Collect into Vec
let vec: Vec<_> = iter.collect();
// Collect into HashMap
let map: HashMap<_, _> = iter.collect();
// Collect Results
let results: Result<Vec<_>, _> = iter.collect();
tokio for Async Runtime#[tokio::main]
async fn main() -> Result<()> {
let result = fetch_data().await?;
Ok(())
}
// BAD - blocks the runtime
async fn bad() {
std::thread::sleep(Duration::from_secs(1));
}
// GOOD - async sleep
async fn good() {
tokio::time::sleep(Duration::from_secs(1)).await;
}
// GOOD - spawn blocking for CPU-intensive work
async fn compute() -> i32 {
tokio::task::spawn_blocking(|| expensive_computation()).await.unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic() {
assert_eq!(add(1, 2), 3);
}
#[test]
fn test_edge_case() {
assert!(validate("").is_err());
}
}
tests/// tests/integration_test.rs
use my_crate::public_api;
#[test]
fn test_full_workflow() {
let result = public_api::process("input");
assert!(result.is_ok());
}
assert! Macros Effectivelyassert!(condition);
assert_eq!(left, right);
assert_ne!(left, right);
assert!(result.is_ok());
assert!(result.is_err());
assert_matches!(value, Pattern::Variant { .. });
// BAD - allocates even if not needed
fn maybe_string() -> String {
String::from("default")
}
// GOOD - return static str when possible
fn maybe_string() -> &'static str {
"default"
}
Vec::with_capacity for Known Sizes// BAD - multiple reallocations
let mut vec = Vec::new();
for i in 0..1000 {
vec.push(i);
}
// GOOD - single allocation
let mut vec = Vec::with_capacity(1000);
for i in 0..1000 {
vec.push(i);
}
cargo build --release
cargo flamegraph # requires cargo-flamegraph
// src/lib.rs
pub mod config;
pub mod client;
pub mod error;
// Re-export public API
pub use config::Config;
pub use client::Client;
pub use error::Error;
pub(crate) for Internal APIs// Public to crate, not external users
pub(crate) fn internal_helper() { ... }
/// Creates a new client with the given configuration.
///
/// # Arguments
///
/// * `config` - The client configuration
///
/// # Errors
///
/// Returns an error if the configuration is invalid.
///
/// # Examples
///
/// ```
/// let client = Client::new(Config::default())?;
/// ```
pub fn new(config: Config) -> Result<Self> {
// ...
}
| Anti-Pattern | Better Approach |
|---|---|
.unwrap() everywhere | Use ? operator |
clone() to satisfy borrow checker | Restructure ownership |
String parameters | Use &str or impl Into<String> |
| Boolean parameters | Use enums |
| Long function bodies | Extract to smaller functions |
| Deep nesting | Use early returns |
| Magic numbers | Use named constants |
# Quality gates
cargo fmt -- --check && cargo clippy -- -D warnings && cargo test
# Common cargo commands
cargo check # Fast syntax/type check
cargo build # Debug build
cargo build --release # Release build
cargo test # Run tests
cargo doc --open # Generate and view docs
cargo clippy --fix # Auto-fix lint issues
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
Generated from the plugin's indexed repository (thrashr888/thrashr888-agent-kit) — no native marketplace selected
npx claudepluginhub p/thrashr888-thrashr888-agent-kit21 skills in this plugin
/agent-ready-engineeringMake codebases ready for reliable human and agent work./building-local-ai-appsDesign patterns for local-first AI desktop apps — provider abstraction (Ollama / OpenAI-compatible gateways), RAG retrieval and ingestion hygiene, citation UX that loops back to sources, streaming and scoped cancellation, chat-loop details, and demo-corpus design. Use when building or reviewing a local AI app, a RAG/notebook product, chat UX, or preparing AI product demos. Distilled from Alchemy (a local NotebookLM clone)./copy-reviewReview user-facing copy (websites, release notes, README, in-app strings) for machine-sounding writing, then codify the owner's voice in a repo WRITING.md. Use when asked to check copy for AI-isms, make text "read human", review marketing or landing-page text, fix writing that "sounds like AI", or set up a writing style guide. Not for project documentation structure (plans/specs/RFCs) — that's style-docs./debugging-tauri-appsFind out why a Tauri app crashed, froze, or failed silently — macOS crash reports, the unified log, the debug bridge's error history, panic hooks, and the capture a shipped app needs to have. Use when a Tauri app misbehaves and the terminal shows nothing, or when adding error/crash logging to one./driving-tauri-appsAutomate, inspect, and verify a running Tauri app with tauri-browser — DOM snapshots, ref-based clicks, React-safe fills, screenshots, direct Tauri command invokes, and demo-data seeding. Use when driving a Tauri app for testing, screenshots, demo setup, or end-to-end verification of app changes./github-releasesCreate releases for Rust CLI tools, macOS apps, Python/uv projects, and Next.js apps. Use when asked to release, tag, ship, publish, or deploy any project.