Expand description
Format trait for an empty format, {}
.
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.
Examples
Implementing Display
on a type:
use std::fmt;
struct Point {
x: i32,
y: i32,
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
let origin = Point { x: 0, y: 0 };
assert_eq!(format!("The origin is: {}", origin), "The origin is: (0, 0)");
Required methods
Formats the value using the given formatter.
Examples
use std::fmt;
struct Position {
longitude: f32,
latitude: f32,
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.longitude, self.latitude)
}
}
assert_eq!("(1.987, 2.983)",
format!("{}", Position { longitude: 1.987, latitude: 2.983, }));
Trait Implementations
Implementations on Foreign Types
1.56.0 · sourceimpl Display for WriterPanicked
impl Display for WriterPanicked
1.20.0 · sourceimpl<'_, T> Display for MutexGuard<'_, T> where
T: Display + ?Sized,
impl<'_, T> Display for MutexGuard<'_, T> where
T: Display + ?Sized,
sourceimpl<W> Display for IntoInnerError<W>
impl<W> Display for IntoInnerError<W>
sourceimpl<T> Display for PoisonError<T>
impl<T> Display for PoisonError<T>
1.17.0 · sourceimpl Display for FromBytesWithNulError
impl Display for FromBytesWithNulError
sourceimpl Display for TryRecvError
impl Display for TryRecvError
1.7.0 · sourceimpl Display for IntoStringError
impl Display for IntoStringError
1.60.0 · sourceimpl Display for ErrorKind
impl Display for ErrorKind
sourcepub fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>
Shows a human-readable description of the ErrorKind
.
This is similar to impl Display for Error
, but doesn’t require first converting to Error.
Examples
use std::io::ErrorKind;
assert_eq!("entity not found", ErrorKind::NotFound.to_string());
1.15.0 · sourceimpl Display for RecvTimeoutError
impl Display for RecvTimeoutError
sourceimpl<'a, K, V> Display for OccupiedError<'a, K, V> where
K: Debug,
V: Debug,
impl<'a, K, V> Display for OccupiedError<'a, K, V> where
K: Debug,
V: Debug,
sourceimpl<T> Display for TrySendError<T>
impl<T> Display for TrySendError<T>
sourceimpl Display for Ipv6Addr
impl Display for Ipv6Addr
Write an Ipv6Addr, conforming to the canonical style described by RFC 5952.
1.7.0 · sourceimpl Display for StripPrefixError
impl Display for StripPrefixError
1.8.0 · sourceimpl Display for SystemTimeError
impl Display for SystemTimeError
1.20.0 · sourceimpl<'_, T> Display for RwLockReadGuard<'_, T> where
T: Display + ?Sized,
impl<'_, T> Display for RwLockReadGuard<'_, T> where
T: Display + ?Sized,
1.4.0 · sourceimpl Display for AddrParseError
impl Display for AddrParseError
1.26.0 · sourceimpl Display for AccessError
impl Display for AccessError
sourceimpl Display for SocketAddr
impl Display for SocketAddr
sourceimpl Display for ExitStatusError
impl Display for ExitStatusError
sourceimpl Display for ExitStatus
impl Display for ExitStatus
sourceimpl Display for JoinPathsError
impl Display for JoinPathsError
1.58.0 · sourceimpl Display for FromVecWithNulError
impl Display for FromVecWithNulError
sourceimpl Display for SocketAddrV4
impl Display for SocketAddrV4
1.20.0 · sourceimpl<'_, T> Display for RwLockWriteGuard<'_, T> where
T: Display + ?Sized,
impl<'_, T> Display for RwLockWriteGuard<'_, T> where
T: Display + ?Sized,
sourceimpl<T> Display for TryLockError<T>
impl<T> Display for TryLockError<T>
sourceimpl Display for SocketAddrV6
impl Display for SocketAddrV6
1.34.0 · sourceimpl Display for TryFromIntError
impl Display for TryFromIntError
1.34.0 · sourceimpl<'a> Display for EscapeDefault<'a>
impl<'a> Display for EscapeDefault<'a>
1.28.0 · sourceimpl Display for NonZeroU128
impl Display for NonZeroU128
1.34.0 · sourceimpl Display for NonZeroI128
impl Display for NonZeroI128
1.20.0 · sourceimpl Display for ParseCharError
impl Display for ParseCharError
1.36.0 · sourceimpl Display for TryFromSliceError
impl Display for TryFromSliceError
1.9.0 · sourceimpl Display for DecodeUtf16Error
impl Display for DecodeUtf16Error
1.28.0 · sourceimpl Display for NonZeroU64
impl Display for NonZeroU64
sourceimpl Display for ParseBoolError
impl Display for ParseBoolError
sourceimpl Display for FromFloatSecsError
impl Display for FromFloatSecsError
1.34.0 · sourceimpl Display for NonZeroI32
impl Display for NonZeroI32
sourceimpl Display for ParseFloatError
impl Display for ParseFloatError
1.34.0 · sourceimpl Display for NonZeroI64
impl Display for NonZeroI64
1.28.0 · sourceimpl Display for NonZeroU16
impl Display for NonZeroU16
1.34.0 · sourceimpl Display for NonZeroIsize
impl Display for NonZeroIsize
1.16.0 · sourceimpl Display for ToUppercase
impl Display for ToUppercase
sourceimpl<T> Display for Saturating<T> where
T: Display,
impl<T> Display for Saturating<T> where
T: Display,
1.39.0 · sourceimpl Display for EscapeDefault
impl Display for EscapeDefault
1.34.0 · sourceimpl<'a> Display for EscapeUnicode<'a>
impl<'a> Display for EscapeUnicode<'a>
1.59.0 · sourceimpl Display for TryFromCharError
impl Display for TryFromCharError
1.16.0 · sourceimpl Display for EscapeDefault
impl Display for EscapeDefault
1.28.0 · sourceimpl Display for LayoutError
impl Display for LayoutError
1.60.0 · sourceimpl<'a> Display for EscapeAscii<'a>
impl<'a> Display for EscapeAscii<'a>
sourceimpl Display for ParseIntError
impl Display for ParseIntError
sourceimpl Display for AllocError
impl Display for AllocError
1.16.0 · sourceimpl Display for EscapeUnicode
impl Display for EscapeUnicode
1.13.0 · sourceimpl Display for BorrowMutError
impl Display for BorrowMutError
1.34.0 · sourceimpl<'a> Display for EscapeDebug<'a>
impl<'a> Display for EscapeDebug<'a>
1.13.0 · sourceimpl Display for BorrowError
impl Display for BorrowError
1.34.0 · sourceimpl Display for Infallible
impl Display for Infallible
1.20.0 · sourceimpl Display for EscapeDebug
impl Display for EscapeDebug
1.34.0 · sourceimpl Display for CharTryFromError
impl Display for CharTryFromError
1.16.0 · sourceimpl Display for ToLowercase
impl Display for ToLowercase
1.28.0 · sourceimpl Display for NonZeroU32
impl Display for NonZeroU32
1.34.0 · sourceimpl Display for NonZeroI16
impl Display for NonZeroI16
1.28.0 · sourceimpl Display for NonZeroUsize
impl Display for NonZeroUsize
1.57.0 · sourceimpl Display for TryReserveError
impl Display for TryReserveError
sourceimpl Display for FromUtf16Error
impl Display for FromUtf16Error
sourceimpl<'_, B> Display for Cow<'_, B> where
B: Display + ToOwned + ?Sized,
<B as ToOwned>::Owned: Display,
impl<'_, B> Display for Cow<'_, B> where
B: Display + ToOwned + ?Sized,
<B as ToOwned>::Owned: Display,
sourceimpl Display for FromUtf8Error
impl Display for FromUtf8Error
sourceimpl<const CAP: usize> Display for ArrayString<CAP>
impl<const CAP: usize> Display for ArrayString<CAP>
sourceimpl<T> Display for CapacityError<T>
impl<T> Display for CapacityError<T>
sourceimpl<'a> Display for Unexpected<'a>
impl<'a> Display for Unexpected<'a>
sourceimpl Display for ParseLevelError
impl Display for ParseLevelError
sourceimpl Display for ParseLevelFilterError
impl Display for ParseLevelFilterError
sourceimpl<T> Display for DisplayValue<T> where
T: Display,
impl<T> Display for DisplayValue<T> where
T: Display,
sourceimpl Display for SetGlobalDefaultError
impl Display for SetGlobalDefaultError
sourceimpl Display for LevelFilter
impl Display for LevelFilter
sourceimpl Display for LevelFilter
impl Display for LevelFilter
sourceimpl Display for SetLoggerError
impl Display for SetLoggerError
sourceimpl Display for ParseLevelError
impl Display for ParseLevelError
sourceimpl Display for ParseError
impl Display for ParseError
sourceimpl Display for TryInitError
impl Display for TryInitError
sourceimpl Display for FromEnvError
impl Display for FromEnvError
sourceimpl<E> Display for FormattedFields<E>
impl<E> Display for FormattedFields<E>
sourceimpl Display for CaseFoldError
impl Display for CaseFoldError
sourceimpl Display for Hir
impl Display for Hir
Print a display representation of this Hir.
The result of this is a valid regular expression pattern string.
This implementation uses constant stack space and heap space proportional
to the size of the Hir
.
sourceimpl Display for UnicodeWordError
impl Display for UnicodeWordError
sourceimpl Display for Ast
impl Display for Ast
Print a display representation of this Ast.
This does not preserve any of the original whitespace formatting that may have originally been present in the concrete syntax from which this Ast was generated.
This implementation uses constant stack space and heap space proportional
to the size of the Ast
.
sourceimpl<'a> Display for ANSIGenericString<'a, str>
impl<'a> Display for ANSIGenericString<'a, str>
sourceimpl<'a> Display for ANSIGenericStrings<'a, str>
impl<'a> Display for ANSIGenericStrings<'a, str>
sourceimpl Display for Value
impl Display for Value
sourcepub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Display a JSON value as a string.
let json = json!({ "city": "London", "street": "10 Downing Street" });
// Compact format:
//
// {"city":"London","street":"10 Downing Street"}
let compact = format!("{}", json);
assert_eq!(compact,
"{\"city\":\"London\",\"street\":\"10 Downing Street\"}");
// Pretty format:
//
// {
// "city": "London",
// "street": "10 Downing Street"
// }
let pretty = format!("{:#}", json);
assert_eq!(pretty,
"{\n \"city\": \"London\",\n \"street\": \"10 Downing Street\"\n}");
sourceimpl<'a, R, T> Display for MappedMutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + Display + ?Sized,
impl<'a, R, T> Display for MappedMutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + Display + ?Sized,
sourceimpl<'a, R, T> Display for RwLockUpgradableReadGuard<'a, R, T> where
R: 'a + RawRwLockUpgrade,
T: 'a + Display + ?Sized,
impl<'a, R, T> Display for RwLockUpgradableReadGuard<'a, R, T> where
R: 'a + RawRwLockUpgrade,
T: 'a + Display + ?Sized,
sourceimpl<'a, R, T> Display for MappedRwLockReadGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Display + ?Sized,
impl<'a, R, T> Display for MappedRwLockReadGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Display + ?Sized,
sourceimpl<'a, R, G, T> Display for ReentrantMutexGuard<'a, R, G, T> where
R: 'a + RawMutex,
G: 'a + GetThreadId,
T: 'a + Display + ?Sized,
impl<'a, R, G, T> Display for ReentrantMutexGuard<'a, R, G, T> where
R: 'a + RawMutex,
G: 'a + GetThreadId,
T: 'a + Display + ?Sized,
sourceimpl<'a, R, T> Display for RwLockReadGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Display + ?Sized,
impl<'a, R, T> Display for RwLockReadGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Display + ?Sized,
sourceimpl<'a, R, T> Display for RwLockWriteGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Display + ?Sized,
impl<'a, R, T> Display for RwLockWriteGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Display + ?Sized,
sourceimpl<'a, R, T> Display for MappedRwLockWriteGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Display + ?Sized,
impl<'a, R, T> Display for MappedRwLockWriteGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Display + ?Sized,
sourceimpl<'a, R, G, T> Display for MappedReentrantMutexGuard<'a, R, G, T> where
R: 'a + RawMutex,
G: 'a + GetThreadId,
T: 'a + Display + ?Sized,
impl<'a, R, G, T> Display for MappedReentrantMutexGuard<'a, R, G, T> where
R: 'a + RawMutex,
G: 'a + GetThreadId,
T: 'a + Display + ?Sized,
sourceimpl<'a, R, T> Display for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + Display + ?Sized,
impl<'a, R, T> Display for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + Display + ?Sized,
sourceimpl Display for CollectionAllocErr
impl Display for CollectionAllocErr
sourceimpl Display for RoundingError
impl Display for RoundingError
sourceimpl<'a, I, B> Display for DelayedFormat<I> where
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
impl<'a, I, B> Display for DelayedFormat<I> where
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
sourceimpl Display for NaiveTime
impl Display for NaiveTime
The Display
output of the naive time t
is the same as
t.format("%H:%M:%S%.f")
.
The string printed can be readily parsed via the parse
method on str
.
It should be noted that, for leap seconds not on the minute boundary, it may print a representation not distinguishable from non-leap seconds. This doesn’t matter in practice, since such leap seconds never happened. (By the time of the first leap second on 1972-06-30, every time zone offset around the world has standardized to the 5-minute alignment.)
Example
use chrono::NaiveTime;
assert_eq!(format!("{}", NaiveTime::from_hms(23, 56, 4)), "23:56:04");
assert_eq!(format!("{}", NaiveTime::from_hms_milli(23, 56, 4, 12)), "23:56:04.012");
assert_eq!(format!("{}", NaiveTime::from_hms_micro(23, 56, 4, 1234)), "23:56:04.001234");
assert_eq!(format!("{}", NaiveTime::from_hms_nano(23, 56, 4, 123456)), "23:56:04.000123456");
Leap seconds may also be used.
assert_eq!(format!("{}", NaiveTime::from_hms_milli(6, 59, 59, 1_500)), "06:59:60.500");
sourceimpl Display for ParseError
impl Display for ParseError
sourceimpl Display for FixedOffset
impl Display for FixedOffset
sourceimpl Display for NaiveDateTime
impl Display for NaiveDateTime
The Display
output of the naive date and time dt
is the same as
dt.format("%Y-%m-%d %H:%M:%S%.f")
.
It should be noted that, for leap seconds not on the minute boundary, it may print a representation not distinguishable from non-leap seconds. This doesn’t matter in practice, since such leap seconds never happened. (By the time of the first leap second on 1972-06-30, every time zone offset around the world has standardized to the 5-minute alignment.)
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd(2016, 11, 15).and_hms(7, 39, 24);
assert_eq!(format!("{}", dt), "2016-11-15 07:39:24");
Leap seconds may also be used.
let dt = NaiveDate::from_ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_500);
assert_eq!(format!("{}", dt), "2015-06-30 23:59:60.500");
sourceimpl Display for NaiveDate
impl Display for NaiveDate
The Display
output of the naive date d
is the same as
d.format("%Y-%m-%d")
.
The string printed can be readily parsed via the parse
method on str
.
Example
use chrono::NaiveDate;
assert_eq!(format!("{}", NaiveDate::from_ymd(2015, 9, 5)), "2015-09-05");
assert_eq!(format!("{}", NaiveDate::from_ymd( 0, 1, 1)), "0000-01-01");
assert_eq!(format!("{}", NaiveDate::from_ymd(9999, 12, 31)), "9999-12-31");
ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.
assert_eq!(format!("{}", NaiveDate::from_ymd( -1, 1, 1)), "-0001-01-01");
assert_eq!(format!("{}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");