anyhow
anyhow provides anyhow::Result<T> and context attachments for application binaries where error taxonomy is not part of the public API.
Search across all documentation pages
anyhow provides anyhow::Result<T> and context attachments for application binaries where error taxonomy is not part of the public API.
[dependencies]
anyhow = "1"use anyhow::{Context, Result};
fn read_port(path: &str) -> Result<u16> {
let s = std::fs::read_to_string(path)
.with_context(|| format!("reading {path}"))?;
let port: u16 = s.trim().parse().context("parsing port")?;
Ok(port)
}When to reach for this: CLIs, servers, and binaries - not published library error types.
fn main() -> anyhow::Result<()> {
let port = read_port("PORT")?;
println!("port={port}");
Ok(())
}What this demonstrates:
main returns anyhow::Result? propagates with context chainanyhow::Error is Send + Sync + 'static, supports downcasting in tests via downcast_ref.
thiserror enum.with_context(|| ...) lazy. Fix: Use context for static strings.? with From or .map_err(Into::into).RUST_BACKTRACE + anyhow feature flags.| Alternative | Use When | Don't Use When |
|---|---|---|
thiserror | Library crates | Quick internal tool |
eyre | Similar app errors | Team uses anyhow |
Box<dyn Error> | Minimal dep | Context chain wanted |
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026