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 AsciiCharimpl Display for Errorimpl Display for ParseFloatErrorimpl Display for BorrowMutErrorimpl Display for ToUppercaseimpl<T: fmt::Display> Display for Wrapping<T>impl Display for TryFromSliceErrorimpl Display for f32impl Display for u8impl<F> Display for FromFn<F>impl Display for i16impl Display for u64impl Display for isizeimpl<T: PointeeSized + Display> Display for &Timpl Display for boolimpl Display for PanicInfo<'_>impl<'a> Display for EscapeAscii<'a>impl Display for BorrowErrorimpl Display for ParseBoolErrorimpl Display for EscapeUnicodeimpl<'a> Display for EscapeUnicode<'a>impl Display for AllocErrorimpl Display for AddrParseErrorimpl Display for ParseCharErrorimpl Display for SocketAddrimpl Display for ParseIntErrorimpl Display for Ipv4Addrimpl Display for FromBytesWithNulErrorimpl<T> Display for NonZero<T>impl Display for DecodeUtf16Errorimpl Display for TryFromCharErrorimpl Display for f16impl Display for u16impl Display for i32impl Display for usizeimpl Display for ToTitlecaseimpl Display for i128impl Display for neverimpl Display for charimpl Display for Utf8Errorimpl<'a> Display for EscapeDefault<'a>impl Display for LayoutErrorimpl Display for Arguments<'_>impl Display for SocketAddrV4impl<T: ?Sized + fmt::Display> Display for Ref<'_, T>impl Display for IpAddrimpl Display for Ipv6Addrimpl<T: ?Sized + fmt::Display> Display for RefMut<'_, T>impl Display for EscapeDebugimpl Display for PanicMessage<'_>impl Display for ByteStrimpl Display for EscapeDefaultimpl Display for f64impl Display for EscapeDefaultimpl<Ptr: fmt::Display> Display for Pin<Ptr>impl Display for i8impl Display for u32impl Display for Location<'_>impl Display for i64impl Display for SocketAddrV6impl Display for u128impl<T: PointeeSized + Display> Display for &mut Timpl Display for CharTryFromErrorimpl Display for strimpl Display for GetDisjointMutErrorimpl<'a> Display for EscapeDebug<'a>impl Display for TryFromFloatSecsErrorimpl Display for ToLowercaseimpl<T: fmt::Display> Display for Saturating<T>impl Display for FromBytesUntilNulErrorimpl Display for TryFromIntErrorimpl Display for Infallible