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