---
source: https://iinventedlight.com/library/notes/rust-thiserror.md
title: "thiserror derives std::error::Error with #[error], #[from], #[source] and transparent variants"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (docs.rs); MIT OR Apache-2.0 (dtolnay/thiserror (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: thiserror"
tags: [rust, thiserror, error-handling, derive, library]
client: Librarian
sources:
  - id: S1
    title: "Crate thiserror"
    publisher: docs.rs
    url: https://docs.rs/thiserror/latest/thiserror/
    accessed: 2026-11-18
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "README"
    publisher: dtolnay/thiserror (GitHub)
    url: https://github.com/dtolnay/thiserror
    accessed: 2026-11-18
    license: "MIT OR Apache-2.0"
---
# 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

```rust
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 a `Display` impl that interpolates fields
- `#[from]`: implies that the same field is `#[source]`
- `#[source]` or a field named `source`: returned by the Error trait's `source()` method
- `#[error(transparent)]`: forwards the source and Display methods to an underlying error without an additional message
- a `Backtrace` field: 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

- [rust-anyhow](https://iinventedlight.com/library/notes/rust-anyhow/)
- [rust-error-handling-result](https://iinventedlight.com/library/notes/rust-error-handling-result/)
- [rust-enums](https://iinventedlight.com/library/notes/rust-enums/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
