anyhow::Result with ? and .context() gives easy error handling in Rust applications
Answer: Use anyhow::Result<T> as the return type of any fallible function, propagate errors with ?, and attach context with .context("...") or .with_context(|| ...). Build one-off errors with anyhow! and return early with bail!.
Example
use anyhow::{anyhow, bail, Context, Result};
fn get_cluster_info() -> Result<ClusterMap> {
let config = std::fs::read_to_string("cluster.json")
.context("Failed to read cluster.json")?;
let map: ClusterMap = serde_json::from_str(&config)?;
if map.is_empty() {
bail!("Missing attribute: {}", "nodes");
}
Ok(map)
}
Error with context:
Error: Failed to read instrs from ./path/to/instrs.json
Caused by:
No such file or directory (os error 2)
Key points
anyhow::Erroris a trait object based error type;?works for any error that implements thestd::error::Errortrait.- Without more context about what higher level step the application was in, an error like "No such file or directory" can be annoying to debug.
- Downcasting is supported by value, by shared reference, or by mutable reference, for example
downcast_ref::<DataStoreError>(). - On Rust 1.65 or newer, a backtrace is captured if the underlying error type does not already provide its own. Set
RUST_BACKTRACE=1for panics and errors, orRUST_LIB_BACKTRACE=1for only errors. - In no_std mode, disable the default "std" feature; a global allocator is required.
anyhow or thiserror
Use Anyhow if you don't care what error type your functions return, which is common in application code. Use thiserror in a library that wants dedicated error types.
See also
Claims
- id: C1 claim: anyhow::Error is a trait object based error type. source: S1 evidence: "a trait object based error type for easy idiomatic error handling"
- id: C2 claim: bail! is shorthand for an early return. source: S2 evidence: "A
bail!macro is provided as a shorthand for the same early return."
- id: C3 claim: Anyhow suits application code. source: S2 evidence: "This is common in application code."
Sources
- [S1] Crate anyhow. docs.rs. https://docs.rs/anyhow/latest/anyhow/ . License: MIT OR Apache-2.0. Accessed 2026-11-18.
- [S2] README. dtolnay/anyhow (GitHub). https://github.com/dtolnay/anyhow . License: MIT OR Apache-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: docs.rs, Crate anyhow; dtolnay/anyhow (GitHub), README. MIT OR Apache-2.0 (https://raw.githubusercontent.com/dtolnay/anyhow/master/LICENSE-APACHE). Copyright David Tolnay. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: docs.rs, Crate anyhow; dtolnay/anyhow (GitHub), README. MIT OR Apache-2.0 (https://raw.githubusercontent.com/dtolnay/anyhow/master/LICENSE-APACHE). Copyright David Tolnay.
Cite this
iinventedlight. 2026. "anyhow::Result with ? and .context() gives easy error handling in Rust applications." iinventedlight Library, iil:note:wx0jhcs6, version of 2026-11-18. https://iinventedlight.com/library/notes/rust-anyhow/. License: CC-BY-4.0.
@misc{iil_note_wx0jhcs6,
author = {{iinventedlight}},
title = {{anyhow::Result with ? and .context() gives easy error handling in Rust applications}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/rust-anyhow/},
note = {Permanent ID iil:note:wx0jhcs6. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-anyhow.md.