🦀 Rust Intermediate

What are the thiserror and anyhow crates for error handling?

Answer

thiserror is used for library crate error handling. It provides a #[derive(Error)] macro that generates the std::error::Error and Display implementations for your custom error enum, eliminating the boilerplate. Each variant can have a #[error("message")] attribute and #[from] to auto-implement From conversions. anyhow is used for application (binary) error handling. Its anyhow::Error type is a type-erased error box that can hold any error implementing std::error::Error, with automatic context chaining via .context("what failed"). The philosophy: use thiserror in library crates where callers need to match on specific error variants, and anyhow in application code where you just need to propagate and display errors clearly.