rust-error-handling

Implement Rust error handling with Result, Option, thiserror, and anyhow.

Updated Dec 13, 2025
One-click install
npx skills add https://github.com/imehr/vet --skill rust-error-handling-imehr
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-error-handling
Source: https://github.com/imehr/vet/tree/main/.claude/skills/rust-error-handling
Command: npx skills add https://github.com/imehr/vet --skill rust-error-handling-imehr

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill guides Rust developers in implementing robust error handling using Result, Option, thiserror, and anyhow to build reliable libraries and applications.

Core Features & Use Cases

  • Patterns for library errors with thiserror and application errors with anyhow to provide clear error types and contexts.
  • Propagation and mapping of errors with the ? operator, context enrichment, and conversions between Option and Result.
  • Real-world scenarios include building CLI tools, services, and crates that require consistent error reporting and user-facing messages.

Quick Start

Create a new Rust project or open an existing one and add dependencies in Cargo.toml: thiserror and anyhow. Create a simple error enum with thiserror and propagate errors using ? in a function that reads a file and parses JSON. Run the code to observe the error context and the error chain produced by anyhow.

Frequently Asked Questions about rust-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I handle errors in Rust using thiserror and anyhow?

Rust error handling with thiserror and anyhow involves defining structured error types for libraries and adding contextual messages for applications. You use thiserror to create custom error enums and anyhow to wrap and propagate errors with rich context.

What is the best way to propagate errors in Rust applications?

The best way to propagate errors in Rust applications is using the ? operator alongside anyhow. This allows you to easily bubble up errors while enriching them with helpful context, ensuring reliable error reporting across services and CLI tools.

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

Use thiserror for building libraries where you need clear, structured error types for consumers, and use anyhow for applications where you need flexible error handling and context enrichment without defining specific error enums.

How do I convert between Option and Result in Rust?

Converting between Option and Result in Rust involves mapping values to handle missing data or errors effectively. The Skill provides patterns for these conversions, ensuring robust propagation and mapping within your application logic.

How do I structure error types for a Rust CLI tool?

Structuring error types for a Rust CLI tool requires defining clear error enums with thiserror and wrapping them with anyhow. This approach provides consistent error reporting and presents helpful, user-facing messages when operations like file reading or JSON parsing fail.

Do I need to add thiserror and anyhow to Cargo.toml for Rust error handling?

Yes, you must add thiserror and anyhow as dependencies in your Cargo.toml to implement these Rust error handling patterns. This setup allows you to structure error types, propagate errors with ?, and present helpful context to users.