From fd691d94824897fb47821c1818eef9ad05e288a9 Mon Sep 17 00:00:00 2001 From: SatvikMishra08 Date: Sun, 27 Sep 2026 11:39:37 +0000 Subject: [PATCH] docs: show Display forwarding in #[from] example Fixes #135. Clarify that #[error("... {0}")] interpolates the #[from] source's Display output, matching the common Disconnect pattern. --- README.md | 6 +++++- src/lib.rs | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7c67cb2f..418785f0 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum DataStoreError { - #[error("data store disconnected")] + #[error("data store disconnected: {0}")] Disconnect(#[from] io::Error), #[error("the data for key `{0}` is not available")] Redaction(String), @@ -93,6 +93,10 @@ pub enum DataStoreError { source error (and possibly a backtrace — see below). Usually `#[from]` fields are unnamed, but `#[from]` is allowed on a named field too. + To include the source's `Display` output in your error message, use `{0}` + (or `{var}` for a named field) in the `#[error("...")]` format string, as in + the `Disconnect` example above. + ```rust #[derive(Error, Debug)] pub enum MyError { diff --git a/src/lib.rs b/src/lib.rs index 9e79f673..b76cbff3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ //! //! #[derive(Error, Debug)] //! pub enum DataStoreError { -//! #[error("data store disconnected")] +//! #[error("data store disconnected: {0}")] //! Disconnect(#[from] io::Error), //! #[error("the data for key `{0}` is not available")] //! Redaction(String), @@ -104,6 +104,10 @@ //! `#[from]` fields are unnamed, but `#[from]` is allowed on a named field //! too. //! +//! To include the source's [`Display`] output in your error message, use +//! `{0}` (or `{var}` for a named field) in the `#[error("...")]` format +//! string, as in the `Disconnect` example above. +//! //! ```rust //! # use core::fmt::{self, Display}; //! # use std::io;