Agent skill

rust

Robust Rust patterns for file-backed data, parsing, persistence, FFI boundaries, and system integration. Use when writing Rust that handles file formats, subprocess integration, PID/process management, Serde serialization, or UniFFI boundaries. Covers UTF-8 safety, atomic writes, state machines, and defensive error handling.

Stars 3
Forks 1

Install this agent skill to your Project

npx add-skill https://github.com/petekp/agent-skills/tree/main/skills/rust

SKILL.md

Rust Engineering Guide

Patterns for building reliable Rust systems that handle file-backed data, external process integration, and cross-language boundaries.

Core Philosophy

Conservative by Default: Inputs from files, subprocesses, and external systems are untrusted.

  • Prefer false negatives over false positives
  • Same input → same output (deterministic)
  • Never panic on user machines due to bad input

Canonical Model Ownership: When Rust is the source of truth, maintain separate representations:

Layer Purpose Characteristics
Internal domain Business logic Expressive enums, rich types
FFI DTOs Cross-language boundary Flat, stable, String-heavy
File format Persistence Versioned, round-trippable
External input Validation boundary Strictly validated, never trusted

Safe Rust Only: None of these patterns require unsafe. Use ecosystem crates for safe abstractions.


Reference Guides

Load the relevant reference when working in that domain:

Domain Reference When to Load
Data Modeling references/data-modeling.md Serde patterns, UniFFI, strong types, versioned schemas
File I/O references/file-io.md Atomic writes, concurrency control, file watching
Process Integration references/process-integration.md PID verification, subprocess handling, timestamps
Text & Parsing references/text-and-parsing.md UTF-8 safety, path normalization, state machines
Testing references/testing.md Round-trip tests, fuzz testing, Clippy lints

Error Handling

Library vs Application Errors

Libraries (public API): Use thiserror with granular error types per operation:

rust
// File operations have their own error type
#[derive(thiserror::Error, Debug)]
pub enum ReadError {
    #[error("failed to read {path}")]
    Io { path: PathBuf, #[source] source: std::io::Error },

    #[error("parse error at line {line}: {message}")]
    Parse { line: usize, message: String },
}

// Subprocess operations have their own error type
#[derive(thiserror::Error, Debug)]
pub enum SubprocessError {
    #[error("failed to spawn process")]
    Spawn(#[source] std::io::Error),

    #[error("process exited with {code:?}: {stderr}")]
    NonZeroExit { code: Option<i32>, stderr: String },

    #[error("output not valid UTF-8")]
    InvalidUtf8(#[source] std::str::Utf8Error),

    #[error("timed out after {0:?}")]
    Timeout(std::time::Duration),
}

Applications (internal/binary): Use anyhow for context-rich errors:

rust
use anyhow::{Context, Result};

fn load_config(path: &Path) -> Result<Config> {
    let content = std::fs::read_to_string(path)
        .with_context(|| format!("failed to read config from {}", path.display()))?;
    // ...
}

Graceful Degradation

Errors degrade functionality, not crash. But log when being lenient:

rust
match parse_metadata(&line) {
    Ok(meta) => entries.push(meta),
    Err(e) => {
        tracing::warn!("skipping malformed entry at line {}: {}", line_num, e);
        // Continue processing other entries
    }
}

Quick Reference

Do

  • Use std::sync::LazyLock for static regex (Rust 1.80+)
  • Hold locks across entire read-modify-write cycles
  • Add #[serde(deny_unknown_fields)] for strict external input
  • Truncate strings with .chars() or graphemes, not byte slicing
  • Write files atomically with sync_all() before rename
  • Verify PID identity with process start time
  • Use saturating_sub for time arithmetic
  • Run cargo clippy -- -D warnings and cargo fmt before commit

Don't

  • Use #[from] without adding context (loses which file failed)
  • Create monolithic error enums spanning unrelated operations
  • Silently ignore errors without logging
  • Slice strings with &s[..N] (panics on char boundaries)
  • Assume directory iteration order is stable
  • Trust subprocess output without validation
  • Use unsafe (not needed for these patterns)

Bugs This Guide Prevents

Bug Pattern Reference
PID reuse "ghost sessions" Store + verify process start time process-integration.md
Timestamp unit mismatch (sec vs ms) Normalize on read process-integration.md
UTF-8 panic on truncation Use .chars().take(n) text-and-parsing.md
Lost updates under concurrency Lock spans full read-modify-write file-io.md
Corrupt file on power loss sync_all() before rename file-io.md
Silent metadata corruption Anchor updates to heading lines text-and-parsing.md
Old data breaks new code #[serde(default)] + alias data-modeling.md

Change Checklist

When modifying these systems, verify:

Schema / Serde

  • New fields use Option + #[serde(default)]
  • Old field names supported via alias (read) or rename (write)
  • External input uses #[serde(deny_unknown_fields)]

Concurrency

  • Mutex held across entire read-modify-write cycle
  • Shared state uses Mutex<T>, not thread_local!
  • File locking documents platform caveats if used

Robustness

  • No panics on file I/O or parse errors
  • Errors logged before being ignored
  • Subprocesses have timeouts

Quality

  • cargo clippy -- -D warnings passes
  • cargo fmt --check passes
  • No unsafe blocks (unless justified and audited)

Expand your agent's capabilities with these related and highly-rated skills.

petekp/agent-skills

multi-model-meta-analysis

Synthesize outputs from multiple AI models into a comprehensive, verified assessment. Use when: (1) User pastes feedback/analysis from multiple LLMs (Claude, GPT, Gemini, etc.) about code or a project, (2) User wants to consolidate model outputs into a single reliable document, (3) User needs conflicting model claims resolved against actual source code. This skill verifies model claims against the codebase, resolves contradictions with evidence, and produces a more reliable assessment than any single model.

3 1
Explore
petekp/agent-skills

capture-learning

Analyze recent conversation context and capture learnings to project knowledge files (for project-specific insights) or skills/commands/subagents (for cross-project patterns). Use when the user asks to "capture this learning", "update the docs with this", "remember this for next time", "document this issue", "add this to CLAUDE.md", "save this knowledge", or "update project knowledge". Also triggers after resolving build/setup issues, discovering non-obvious patterns, or completing debugging sessions with valuable insights.

3 1
Explore
petekp/agent-skills

optimize-agent-docs

Build a retrieval-optimized knowledge layer over agent documentation in dotfiles (.claude, .codex, .cursor, .aider). Use when asked to "optimize docs", "improve agent knowledge", "make docs more efficient", or when documentation has accumulated and retrieval feels inefficient. Generates a manifest mapping task-contexts to knowledge chunks, optimizes information density, and creates compiled artifacts for efficient agent consumption.

3 1
Explore
petekp/agent-skills

agent-changelog

Compile an agent-optimized changelog by cross-referencing git history with plans and documentation. Use when asked to "update changelog", "compile history", "document project evolution", or proactively after major milestones, architectural changes, or when stale/deprecated information is detected that could confuse coding agents.

3 1
Explore
petekp/agent-skills

literate-guide

Create a narrative guide to a codebase or feature in the style of Knuth's Literate Programming — code and prose interwoven as a single essay, ordered for human understanding rather than compiler needs. Use when the user asks to 'explain this codebase as a story', 'write a literate guide', 'create a narrative walkthrough', 'tell the story of this code', 'Knuth-style documentation', 'weave a guide for this feature', or when they want deep, readable documentation that treats the program as literature. Also trigger when someone wants a document that a thoughtful reader could follow from start to finish and come away understanding both WHAT the code does and WHY every design choice was made.

3 1
Explore
petekp/agent-skills

autonomous-agent-readiness

Assess a codebase's readiness for autonomous agent development and provide tailored recommendations. Use when asked to evaluate how well a project supports unattended agent execution, assess development practices for agent autonomy, audit infrastructure for agent reliability, or improve a codebase for autonomous agent workflows. Triggers on requests like "assess this project for agent readiness", "how autonomous-ready is this codebase", "evaluate agent infrastructure", or "improve development practices for agents".

3 1
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results