API Reference

anyhow

Flexible error type for Rust applications that adds context to any error and prints causal chains.

Official Documentation ↗
CompatibilityThis documentation covers version v1. Check the official docs for the latest API surface.

Installation

cargocargo add anyhow

Methods

Flexible error type for Rust applications that adds context to any error and prints causal chains.

Overview

anyhow is the error type for application code that values propagation over classification. Any error converts into it with ?, context layers explain what each step was doing, and the debug report prints the full causal chain.

It is deliberately dynamic: variants are erased and downcasting is opt-in when one specific source type matters. That trade keeps binaries simple while libraries beneath keep their typed errors.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • anyhow::Result — The application-wide Result alias where any error converts into anyhow::Error via ?.
  • context / with_context — Attaches a human-readable cause layer to a fallible operation at the call site.
  • anyhow! macro — Creates errors from format strings, and bail! returns early with that error.
  • chain / root_cause — Walks the layered causes for diagnostics and recovers the concrete error type when needed.

Usage Notes

  • Add context at boundaries where the operation is meaningful, file name, endpoint, batch id.
  • Use with_context for messages that would otherwise compute eagerly on the success path.
  • Reserve downcast_ref for the rare place that must branch on a concrete error type.

Common Pitfalls

  • Using anyhow in a public library erases variant information callers need to match on.
  • Context strings without identifying data, “failed to read file”, lose the file name.
  • Display on anyhow prints only the outermost layer; use {:?} for diagnostics.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/dtolnay/anyhow@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:08.696Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

anyhow or thiserror, which do I pick?
Libraries should define concrete error types with thiserror so callers can match on variants. Applications that just propagate and report use anyhow for its ad-hoc context and universal conversion. They combine well: thiserror at the library boundary, anyhow in binaries.
Why print errors with {:?}?
The Debug format renders the full context chain and backtrace, while Display shows only the top message. anyhow main returning Err already uses the debug report, which is why uncaught errors print richly.
How do I get backtraces?
anyhow captures backtraces on nightly or with std backtrace stabilization when RUST_BACKTRACE=1 is set in the environment. The Debug output includes them automatically once enabled.