derive(Display)
This library provides a convenient derive macro for the standard library's
std::fmt::Display
trait. displaythis is a fork of
thiserror, modified for types that are
not errors.
Example
# use io;
use Display;
Details
-
displaythis deliberately does not appear in your public API. You get the same thing as if you had written an implementation of
std::fmt::Display
by hand, and switching from handwritten impls to displaythis or vice versa is not a breaking change. -
Types may be enums, structs with named fields, tuple structs, or unit structs.
-
You should provide
#[display("...")]
messages on the struct or each variant of your enum, as shown above in the example.The messages support a shorthand for interpolating fields from the error.
#[display("{var}")]
⟶write!("{}", self.var)
#[display("{0}")]
⟶write!("{}", self.0)
#[display("{var:?}")]
⟶write!("{:?}", self.var)
#[display("{0:?}")]
⟶write!("{:?}", self.0)
These shorthands can be used together with any additional format args, which may be arbitrary expressions. For example:
If one of the additional expression arguments needs to refer to a field of the struct or enum, then refer to named fields as
.var
and tuple fields as.0
.