thiserror derives std::error::Error with #[error], #[from], #[source] and transparent variants
Answer: Add #[derive(Error, Debug)] to an enum or struct, give each variant an #[error("...")] message, and mark wrapped errors with #[from]. thiserror is a derive macro for the standard library's std::error::Error trait and deliberately does not appear in your public API.
Example
use thiserror::Error;
#[derive(Error, Debug)]
pub enum DataStoreError {
#[error("data store disconnected")]
Disconnect(#[from] io::Error),
#[error("the data for key `{0}` is not available")]
Redaction(String),
#[error("invalid header (expected {expected:?}, found {found:?})")]
InvalidHeader { expected: String, found: String },
#[error(transparent)]
Other(#[from] anyhow::Error),
}
Attributes
#[error("{var}")],#[error("{0:?}")]: generates aDisplayimpl that interpolates fields#[from]: implies that the same field is#[source]#[source]or a field namedsource: returned by the Error trait'ssource()method#[error(transparent)]: forwards the source and Display methods to an underlying error without an additional message- a
Backtracefield: detected automatically
Details
- Errors may be enums, structs with named fields, tuple structs, or unit structs.
- Switching from handwritten impls to thiserror or vice versa is not a breaking change.
error(transparent)is appropriate for enums that need an "anything else" variant.- The
#[backtrace]attribute requires a nightly compiler with Rust version 1.73 or newer. - Cargo.toml:
thiserror = "2"under[dependencies].
thiserror or anyhow
Use thiserror if you care about designing your own dedicated error types, which most often applies to library-like code. Use Anyhow in application-like code.
See also
Claims
- id: C1 claim: thiserror provides a derive macro. source: S1 evidence: "This library provides a convenient derive macro"
- id: C2 claim: #[from] always implies #[source]. source: S2 evidence: "The
#[from]attribute always implies that the same field is#[source]"
- id: C3 claim: transparent forwards source and Display to an underlying error. source: S2 evidence: "to forward the source and Display methods straight through to an underlying error"
Sources
- [S1] Crate thiserror. docs.rs. https://docs.rs/thiserror/latest/thiserror/ . License: MIT OR Apache-2.0. Accessed 2026-11-18.
- [S2] README. dtolnay/thiserror (GitHub). https://github.com/dtolnay/thiserror . 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 thiserror; dtolnay/thiserror (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 thiserror; dtolnay/thiserror (GitHub), README. MIT OR Apache-2.0 (https://raw.githubusercontent.com/dtolnay/anyhow/master/LICENSE-APACHE). Copyright David Tolnay.
Cite this
iinventedlight. 2026. "thiserror derives std::error::Error with error, from, source and transparent attributes." iinventedlight Library, iil:note:t89m2dgq, version of 2026-11-18. https://iinventedlight.com/library/notes/rust-thiserror/. License: CC-BY-4.0.
@misc{iil_note_t89m2dgq,
author = {{iinventedlight}},
title = {{thiserror derives std::error::Error with error, from, source and transparent attributes}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/rust-thiserror/},
note = {Permanent ID iil:note:t89m2dgq. 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).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-thiserror.table.json (table; CC-BY-4.0)
- rust-thiserror.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-thiserror.md.