pub trait Display {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>;
}
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, }));

Implementations on Foreign Types

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

Shows the original regular expression.

Shows the original regular expression.

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.

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.

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

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");

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");

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");

Renders as numer/denom. If denom=1, renders as numer.

Display the name of the address format (not the description).

Implementors