Trait kvarn::prelude::fmt::Display

1.0.0 · source ·
pub trait Display {
    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.


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.

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, }));


Write an Ipv6Addr, conforming to the canonical style described by RFC 5952.

Formats the status code, including the canonical reason.


assert_eq!(format!("{}", StatusCode::OK), "200 OK");

The format returned by this implementation is not stable and must not be relied upon.

By default this produces an exact, full-precision printout of the duration. For a concise, rounded printout instead, you can use the .N format specifier:

let duration = Duration::new(123456, 789011223);
println!("{:.3}", duration);

For the purposes of this implementation, a day is exactly 24 hours and a minute is exactly 60 seconds.

The Display implementation allows the std::error::Error implementation