.claude/skills/rust-expert/SKILL.md
Rust programming expert including ownership, borrowing, lifetimes, async Tokio patterns, error handling, trait system, performance optimization, testing, and production systems development
npx skillsauth add oimiragieo/agent-studio rust-expertInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Apply idiomatic Rust patterns with strong safety, performance, and maintainability guarantees.
// Prefer moves when value is consumed; prefer borrows when value is shared.
fn process(data: Vec<u8>) -> Vec<u8> { /* consumes */ }
fn inspect(data: &[u8]) -> usize { data.len() /* borrows */ }
// Clone only when necessary and explicitly documented.
// Anti-pattern: clone() to silence borrow checker errors — fix the design instead.
Cow<'a, T> when you need borrow-or-own semantics without forcing allocation.use std::borrow::Cow;
fn normalize(s: &str) -> Cow<str> {
if s.contains(' ') {
Cow::Owned(s.replace(' ', "_"))
} else {
Cow::Borrowed(s)
}
}
// Explicit lifetime annotation: only when compiler cannot infer.
struct Parser<'input> {
source: &'input str,
pos: usize,
}
impl<'input> Parser<'input> {
fn next_token(&mut self) -> &'input str {
// Returns a slice of the original input — lifetime ties result to source.
&self.source[self.pos..]
}
}
// RPIT (Return Position Impl Trait) avoids lifetime noise in many cases.
fn words(s: &str) -> impl Iterator<Item = &str> {
s.split_whitespace()
}
clone() inside hot loops to avoid borrow checker.unsafe to bypass lifetime checks — redesign instead.'static bounds that force heap allocation when borrowing suffices.&mut T in structs — prefer owned data or RefCell<T>.| Scenario | Tool |
| --------------------------- | --------------------------------- |
| Library crate errors | thiserror — typed, composable |
| Application / binary errors | anyhow — ergonomic ? chaining |
| Domain-specific context | Custom enum via thiserror |
| Infallible conversions | From / Into — zero overhead |
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("missing required field: {field}")]
MissingField { field: &'static str },
#[error("invalid value for {field}: {source}")]
ParseError { field: &'static str, #[source] source: std::num::ParseIntError },
#[error(transparent)]
Io(#[from] std::io::Error),
}
use anyhow::{Context, Result};
fn load_config(path: &str) -> Result<Config> {
let raw = std::fs::read_to_string(path)
.with_context(|| format!("reading config from {path}"))?;
toml::from_str(&raw).context("parsing config TOML")
}
.unwrap() in library code; use .expect() only in tests and main() where the invariant is self-evident..context() / .with_context() at every error boundary to preserve the call chain.// Prefer generics (monomorphized, zero-cost) when types are known at compile time.
fn serialize<S: Serialize>(value: &S) -> Vec<u8> { /* ... */ }
// Use dyn Trait only when you need heterogeneous collections or runtime dispatch.
fn handlers() -> Vec<Box<dyn EventHandler>> { /* ... */ }
// Prefer where clauses for readability when bounds are complex.
fn merge<K, V>(a: HashMap<K, V>, b: HashMap<K, V>) -> HashMap<K, V>
where
K: Eq + Hash,
V: Clone,
{ /* ... */ }
Display, From, Iterator) for your types.struct Wrapper(Vec<u8>);
impl fmt::Display for Wrapper {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}
// Stable as of Rust 1.75 — no more async-trait macro needed.
trait DataSource {
async fn fetch(&self, id: u64) -> anyhow::Result<Record>;
}
// Return-Position Impl Trait in Traits (RPITIT) — also stable in 1.75.
trait Transformer {
fn transform(&self, input: &str) -> impl Iterator<Item = String>;
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Default: multi-thread runtime (all CPU cores).
Ok(())
}
// Single-thread runtime for I/O-only workloads.
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> { Ok(()) }
use tokio::task;
// Spawn a non-blocking async task.
let handle = task::spawn(async move {
fetch_data(url).await
});
let result = handle.await?; // propagate JoinError
// CPU-bound work must go to the blocking thread pool — never block the async runtime.
let result = task::spawn_blocking(|| {
expensive_cpu_computation()
}).await?;
use tokio::sync::{mpsc, oneshot, broadcast, watch};
// mpsc: producer → consumer pipeline.
let (tx, mut rx) = mpsc::channel::<Bytes>(1024);
// oneshot: request / response pattern.
let (resp_tx, resp_rx) = oneshot::channel::<Result<Record>>();
// broadcast: fan-out to multiple subscribers.
let (tx, _rx) = broadcast::channel::<Event>(16);
// watch: latest-value semantics (config reload, health state).
let (tx, rx) = watch::channel(Config::default());
use tokio::select;
use tokio_util::sync::CancellationToken;
async fn worker(token: CancellationToken) {
select! {
_ = token.cancelled() => {
// Structured cancellation — always handle shutdown path.
}
result = do_work() => {
// Normal completion.
}
}
}
use tokio::task::JoinSet;
async fn fetch_all(urls: Vec<String>) -> Vec<anyhow::Result<Bytes>> {
let mut set = JoinSet::new();
for url in urls {
set.spawn(fetch(url));
}
let mut results = Vec::new();
while let Some(res) = set.join_next().await {
results.push(res.expect("task panicked"));
}
results
}
std::thread::sleep inside async context — use tokio::time::sleep.MutexGuard across .await — use tokio::sync::Mutex instead.spawn_blocking or tokio::fs.use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct User {
pub id: u64,
pub display_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
}
use axum::{extract::{Path, State}, routing::get, Json, Router};
async fn get_user(
State(db): State<DbPool>,
Path(id): Path<u64>,
) -> Result<Json<User>, AppError> {
let user = db.find_user(id).await?;
Ok(Json(user))
}
let app = Router::new()
.route("/users/:id", get(get_user))
.with_state(db_pool);
use sqlx::PgPool;
async fn insert_user(pool: &PgPool, name: &str) -> sqlx::Result<i64> {
let row = sqlx::query!("INSERT INTO users (name) VALUES ($1) RETURNING id", name)
.fetch_one(pool)
.await?;
Ok(row.id)
}
use reqwest::Client;
async fn fetch_json<T: for<'de> serde::Deserialize<'de>>(
client: &Client,
url: &str,
) -> anyhow::Result<T> {
Ok(client.get(url).send().await?.error_for_status()?.json().await?)
}
use rayon::prelude::*;
fn parallel_sum(data: &[f64]) -> f64 {
data.par_iter().sum()
}
use clap::Parser;
#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
#[arg(short, long, default_value = "8080")]
port: u16,
#[arg(short, long, env = "CONFIG_PATH")]
config: std::path::PathBuf,
}
#[inline] for hot, small functions that cross crate boundaries.Box, Vec, Arc) only when necessary.// Use std::simd (nightly) or portable-simd crate for explicit vectorization.
// Profile first — LLVM auto-vectorizes most iterator chains.
use std::simd::{f32x8, SimdFloat};
fn dot_product_simd(a: &[f32], b: &[f32]) -> f32 {
a.chunks_exact(8)
.zip(b.chunks_exact(8))
.map(|(a_chunk, b_chunk)| {
let va = f32x8::from_slice(a_chunk);
let vb = f32x8::from_slice(b_chunk);
(va * vb).reduce_sum()
})
.sum()
}
# flamegraph (install: cargo install flamegraph)
cargo flamegraph --bin my-app
# perf stat for CPU counters (Linux)
perf stat cargo run --release
# heaptrack for heap allocation analysis (Linux)
heaptrack cargo run --release
# criterion for micro-benchmarks
cargo bench
// Preallocate when size is known.
let mut v = Vec::with_capacity(expected_len);
// String building: use write! into a pre-allocated String.
use std::fmt::Write;
let mut s = String::with_capacity(256);
write!(s, "id={}", id)?;
// Avoid format!() in hot paths — prefer direct write!() or push_str().
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_valid_config() {
let cfg = Config::from_str("[server]\nport = 9000").unwrap();
assert_eq!(cfg.port, 9000);
}
#[test]
fn parse_invalid_port_returns_error() {
let result = Config::from_str("[server]\nport = -1");
assert!(result.is_err());
}
}
// tests/integration/server.rs — tests the full public API.
#[tokio::test]
async fn health_endpoint_returns_200() {
let addr = spawn_test_server().await;
let resp = reqwest::get(format!("http://{addr}/health")).await.unwrap();
assert_eq!(resp.status(), 200);
}
use proptest::prelude::*;
proptest! {
#[test]
fn encode_decode_roundtrip(data: Vec<u8>) {
let encoded = encode(&data);
prop_assert_eq!(decode(&encoded).unwrap(), data);
}
}
#[tokio::test]
async fn fetch_returns_data() {
let mock = MockServer::start().await;
Mock::given(method("GET")).respond_with(ResponseTemplate::new(200)).mount(&mock).await;
let result = fetch(&mock.uri()).await;
assert!(result.is_ok());
}
<function>_<scenario>_<expected>.cargo test -- --nocapture for diagnostic output during development.cargo nextest run for faster parallel test execution in CI./// # Safety
///
/// - `ptr` must be non-null and properly aligned for `T`.
/// - The memory at `ptr` must be valid for `len` elements of type `T`.
/// - The caller must ensure no other mutable references to the memory exist.
pub unsafe fn from_raw_parts<T>(ptr: *const T, len: usize) -> &'static [T] {
std::slice::from_raw_parts(ptr, len)
}
unsafe block must have a // SAFETY: comment explaining why it is sound.unsafe — wrap in a safe abstraction immediately.unsafe fn over unsafe block inside a safe fn when the entire function is unsafe.cargo +nightly miri test) to detect undefined behavior in unsafe code.#[no_mangle]
pub extern "C" fn my_add(a: i32, b: i32) -> i32 {
a + b
}
extern "C" {
fn strlen(s: *const std::os::raw::c_char) -> usize;
}
fn rust_strlen(s: &str) -> usize {
let cstr = std::ffi::CString::new(s).expect("no null bytes");
// SAFETY: cstr is a valid, null-terminated C string.
unsafe { strlen(cstr.as_ptr()) }
}
cbindgen to generate C headers from Rust public API.bindgen to generate Rust bindings from C headers.# Cargo.toml (workspace root)
[workspace]
members = ["crates/core", "crates/server", "crates/cli"]
resolver = "2"
[workspace.dependencies]
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
# crates/core/Cargo.toml
[dependencies]
tokio.workspace = true
serde.workspace = true
[features]
default = ["std"]
std = []
async = ["dep:tokio"]
metrics = ["dep:prometheus"]
[dependencies]
tokio = { version = "1", optional = true }
prometheus = { version = "0.13", optional = true }
#[cfg(feature = "metrics")]
fn record_latency(ms: u64) { /* ... */ }
#[cfg(not(feature = "metrics"))]
fn record_latency(_ms: u64) {}
// build.rs — runs before crate compilation.
fn main() {
// Rerun when proto files change.
println!("cargo:rerun-if-changed=proto/");
// Link a system library.
println!("cargo:rustc-link-lib=ssl");
}
cargo build --release # optimized build
cargo clippy -- -D warnings # lint (treat warnings as errors)
cargo fmt --check # format check (CI)
cargo doc --no-deps --open # generate and open docs
cargo audit # check dependencies for CVEs
cargo deny check # license + advisory checks
cargo expand # show macro expansion
// No longer requires #[async_trait] crate.
trait Fetcher {
async fn fetch(&self, url: &str) -> anyhow::Result<Bytes>;
}
trait Source {
fn items(&self) -> impl Iterator<Item = &str>;
}
let Ok(value) = parse_value(raw) else {
return Err(ConfigError::InvalidValue);
};
use std::io::ErrorKind;
match err.kind() {
ErrorKind::NotFound => { /* ... */ }
ErrorKind::PermissionDenied => { /* ... */ }
_ => { /* ... */ }
}
Before marking Rust work complete:
cargo clippy -- -D warnings passes with zero warnings.cargo fmt --check produces no changes.cargo test (or cargo nextest run) passes — all tests green.unsafe blocks have // SAFETY: comments./// doc comments..unwrap() in library code except tests.Debug + implement std::error::Error.This skill is used by:
developer — Rust feature implementation and bug fixescode-reviewer — Rust-specific code review patternsqa — Rust testing strategies (proptest, criterion, nextest)Before starting:
Read .claude/context/memory/learnings.md
After completing:
.claude/context/memory/learnings.md.claude/context/memory/issues.md.claude/context/memory/decisions.mdASSUME INTERRUPTION: Your context may reset. If it's not in memory, it didn't happen.
tools
Comprehensive biosignal processing toolkit for analyzing physiological data including ECG, EEG, EDA, RSP, PPG, EMG, and EOG signals. Use this skill when processing cardiovascular signals, brain activity, electrodermal responses, respiratory patterns, muscle activity, or eye movements. Applicable for heart rate variability analysis, event-related potentials, complexity measures, autonomic nervous system assessment, psychophysiology research, and multi-modal physiological signal integration.
tools
Comprehensive toolkit for creating, analyzing, and visualizing complex networks and graphs in Python. Use when working with network/graph data structures, analyzing relationships between entities, computing graph algorithms (shortest paths, centrality, clustering), detecting communities, generating synthetic networks, or visualizing network topologies. Applicable to social networks, biological networks, transportation systems, citation networks, and any domain involving pairwise relationships.
data-ai
Molecular featurization for ML (100+ featurizers). ECFP, MACCS, descriptors, pretrained models (ChemBERTa), convert SMILES to features, for QSAR and molecular ML.
development
Run Python code in the cloud with serverless containers, GPUs, and autoscaling. Use when deploying ML models, running batch processing jobs, scheduling compute-intensive tasks, or serving APIs that require GPU acceleration or dynamic scaling.