The Display derive macro generates an implementation of the core::fmt::Display trait based on the docstrings (///) of your enum variants or struct fields.
Interpolation Syntax
You can interpolate fields into the display message using the following shorthand in your docstrings:
/// {var}: Interpolates field var using Display (write!("{}", self.var))/// {0}: Interpolates the first field using Display (write!("{}", self.0))/// {var:?}: Interpolates field var using Debug (write!("{:?}", self.var))/// {0:?}: Interpolates the first field using Debug (write!("{:?}", self.0))
Example: Enum with variants
use std::io;
use displaydoc::Display;
use thiserror::Error;
#[derive(Display, Error, Debug)]
pub enum DataStoreError {
/// data store disconnected
Disconnect(#[source] io::Error),
/// the data for key `{0}` is not available
Redaction(String),
/// invalid header (expected {expected:?}, found {found:?})
InvalidHeader {
expected: String,
found: String,
},
/// unknown data store error
Unknown,
}