pub trait Display {
// Required method
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>;
}
Expand description
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 parseabilityDisplay
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.
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.
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)");
1.0.0 · Source
Formats the value using the given formatter.
§ErrorsThis function should return Err
if, and only if, the provided Formatter
returns Err
. String formatting is considered an infallible operation; this function only returns a Result
because 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.
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, }),
);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4