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, }));
Implementors
impl Display for std_tools::dependencies::derive_tools::dependencies::parse_display::helpers::regex::Error
impl Display for std_tools::dependencies::strs_tools::string::number::Error
impl Display for Infallible
impl Display for VarError
impl Display for std::io::error::ErrorKind
impl Display for SocketAddr
impl Display for IpAddr
impl Display for RecvTimeoutError
impl Display for TryRecvError
impl Display for time::ParseError
impl Display for Weekday
impl Display for RoundingError
impl Display for bool
impl Display for char
impl Display for f32
impl Display for f64
impl Display for i8
impl Display for i16
impl Display for i32
impl Display for i64
impl Display for i128
impl Display for isize
impl Display for !
impl Display for str
impl Display for u8
impl Display for u16
impl Display for u32
impl Display for u64
impl Display for u128
impl Display for usize
impl Display for std_tools::dependencies::derive_tools::dependencies::parse_display::helpers::regex::bytes::Regex
impl Display for std_tools::dependencies::derive_tools::dependencies::parse_display::helpers::regex::Regex
impl Display for std_tools::dependencies::derive_tools::dependencies::parse_display::ParseError
impl Display for std_tools::dependencies::error_tools::for_app::Error
impl Display for BasicError
impl Display for TryReserveError
impl Display for FromVecWithNulError
impl Display for IntoStringError
impl Display for NulError
impl Display for FromUtf8Error
impl Display for FromUtf16Error
impl Display for String
impl Display for LayoutError
impl Display for AllocError
impl Display for TryFromSliceError
impl Display for core::ascii::EscapeDefault
impl Display for BorrowError
impl Display for BorrowMutError
impl Display for CharTryFromError
impl Display for ParseCharError
impl Display for DecodeUtf16Error
impl Display for core::char::EscapeDebug
impl Display for core::char::EscapeDefault
impl Display for core::char::EscapeUnicode
impl Display for ToLowercase
impl Display for ToUppercase
impl Display for TryFromCharError
impl Display for FromBytesUntilNulError
impl Display for FromBytesWithNulError
impl Display for core::num::dec2flt::ParseFloatError
impl Display for ParseIntError
impl Display for TryFromIntError
impl Display for NonZeroI8
impl Display for NonZeroI16
impl Display for NonZeroI32
impl Display for NonZeroI64
impl Display for NonZeroI128
impl Display for NonZeroIsize
impl Display for NonZeroU8
impl Display for NonZeroU16
impl Display for NonZeroU32
impl Display for NonZeroU64
impl Display for NonZeroU128
impl Display for NonZeroUsize
impl Display for ParseBoolError
impl Display for Utf8Error
impl Display for Utf8Lossy
impl Display for FromFloatSecsError
impl Display for Backtrace
impl Display for JoinPathsError
impl Display for Report<Box<dyn Error + 'static, Global>>
impl Display for WriterPanicked
impl Display for std::io::error::Error
impl Display for SocketAddrV4
impl Display for SocketAddrV6
impl Display for Ipv4Addr
impl Display for Ipv6Addr
Write an Ipv6Addr, conforming to the canonical style described by RFC 5952.
impl Display for AddrParseError
impl Display for StripPrefixError
impl Display for ExitStatus
impl Display for ExitStatusError
impl Display for RecvError
impl Display for AccessError
impl Display for SystemTimeError
impl Display for Duration
impl Display for OutOfRangeError
impl Display for SteadyTime
impl Display for chrono::format::ParseError
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");
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");
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");
impl Display for FixedOffset
impl Display for Utc
impl Display for num_traits::ParseFloatError
impl Display for std_tools::protected::dt::protected::fmt::Error
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
.
impl Display for CaseFoldError
impl Display for Error
impl Display for Error
impl Display for Error
impl Display for Error
impl Display for ErrorKind
impl Display for ErrorKind
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
.