Trait Display
trait Display: PointeeSized
Format trait for an empty format, {}.
Implementing this trait for a type will automatically implement the
ToString trait for the type, allowing the usage
of the .to_string() method. Prefer implementing
the Display trait for a type, rather than ToString.
Display is similar to Debug, but Display is for user-facing
output, and so cannot be derived.
For more information on formatters, see the module-level documentation.
Completeness and parseability
Display for a type might not necessarily be a lossless or complete representation of the type.
It may omit internal state, precision, or other information the type does not consider important
for user-facing output, as determined by the type. As such, the output of Display might not be
possible to parse, and even if it is, the result of parsing might not exactly match the original
value.
However, if a type has a lossless Display implementation whose output is meant to be
conveniently machine-parseable and not just meant for human consumption, then the type may wish
to accept the same format in FromStr, and document that usage. Having both Display and
FromStr implementations where the result of Display cannot be parsed with FromStr may
surprise users.
Internationalization
Because a type can only have one Display implementation, it is often preferable
to only implement Display when there is a single most "obvious" way that
values can be formatted as text. This could mean formatting according to the
"invariant" culture and "undefined" locale, or it could mean that the type
display is designed for a specific culture/locale, such as developer logs.
If not all values have a justifiably canonical textual format or if you want
to support alternative formats not covered by the standard set of possible
formatting traits, the most flexible approach is display adapters: methods
like str::escape_default or Path::display which create a wrapper
implementing Display to output the specific display format.
Examples
Implementing Display on a type:
use fmt;
let origin = Point ;
assert_eq!;
Required Methods
fn fmt(self: &Self, f: &mut Formatter<'_>) -> ResultFormats the value using the given formatter.
Errors
This function should return
Errif, and only if, the providedFormatterreturnsErr. String formatting is considered an infallible operation; this function only returns aResultbecause writing to the underlying stream might fail and it must provide a way to propagate the fact that an error has occurred back up the stack.Examples
use fmt; assert_eq!;
Implementors
impl Display for u16impl Display for EscapeDefaultimpl Display for BorrowMutErrorimpl<'a> Display for EscapeDebug<'a>impl Display for BorrowErrorimpl Display for isizeimpl Display for PanicInfo<'_>impl Display for ParseIntErrorimpl Display for EscapeDebugimpl Display for strimpl Display for SocketAddrV4impl Display for AsciiCharimpl Display for i16impl Display for TryFromIntErrorimpl<'a> Display for EscapeDefault<'a>impl Display for EscapeUnicodeimpl Display for PanicMessage<'_>impl Display for u128impl Display for TryFromSliceErrorimpl Display for EscapeDefaultimpl Display for f32impl Display for ToUppercaseimpl Display for Arguments<'_>impl Display for charimpl<Ptr: fmt::Display> Display for Pin<Ptr>impl<F> Display for FromFn<F>impl Display for u32impl Display for ParseCharErrorimpl<'a> Display for EscapeUnicode<'a>impl Display for i128impl<T: fmt::Display> Display for Saturating<T>impl Display for TryFromCharErrorimpl Display for Ipv4Addrimpl Display for f64impl Display for DecodeUtf16Errorimpl Display for Location<'_>impl<T: ?Sized + fmt::Display> Display for RefMut<'_, T>impl<'a> Display for EscapeAscii<'a>impl Display for i32impl Display for CharTryFromErrorimpl Display for TryFromFloatSecsErrorimpl Display for Ipv6Addrimpl<T: PointeeSized + Display> Display for &Timpl Display for IpAddrimpl Display for f16impl Display for GetDisjointMutErrorimpl<T> Display for NonZero<T>impl<T: ?Sized + fmt::Display> Display for Ref<'_, T>impl Display for ByteStrimpl Display for u64impl Display for LayoutErrorimpl Display for FromBytesWithNulErrorimpl Display for Infallibleimpl<T: PointeeSized + Display> Display for &mut Timpl Display for AddrParseErrorimpl Display for ToLowercaseimpl Display for u8impl Display for Utf8Errorimpl<T: fmt::Display> Display for Wrapping<T>impl Display for i64impl Display for FromBytesUntilNulErrorimpl Display for SocketAddrV6impl Display for AllocErrorimpl Display for Errorimpl Display for neverimpl Display for i8impl Display for ParseBoolErrorimpl Display for usizeimpl Display for SocketAddrimpl Display for bool