anyhow
Flexible error type for Rust applications that adds context to any error and prints causal chains.
Official Documentation ↗v1. Check the official docs for the latest API surface.Installation
cargo add anyhowMethods
No methods match this filter.
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.
- A low-level error like “No such file or directory” can be annoying to debug without more context about what higher level step the application was in the middle of. (https://cdn.jsdelivr.net/gh/dtolnay/anyhow@latest/README.md)
- Comparison to failure The anyhow::Error type works something like failure::Error, but unlike failure ours is built around the standard library’s std::error::Error trait rather than a separate trait failure::Fail. (https://cdn.jsdelivr.net/gh/dtolnay/anyhow@latest/README.md)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.