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

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

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

Shows the original regular expression.

Shows the original regular expression.

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.

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.

Display the serialization of this URL.

Pretty prints a Request. This is primarily used by Rocket’s logging infrastructure.

Displays the source. Location and custom sources are displayed directly. File paths are displayed relative to the current working directory if the relative path is shorter than the complete path.

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

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

Display self as best as possible. For perfectly custom display output, consider using [ByteUnit::repr()].

Example

use ubyte::{ByteUnit, ToByteUnit};

assert_eq!(323.kilobytes().to_string(), "323kB");
assert_eq!(3.megabytes().to_string(), "3MB");
assert_eq!(3.mebibytes().to_string(), "3MiB");

assert_eq!((3.mebibytes() + 140.kilobytes()).to_string(), "3.13MiB");
assert_eq!((3.mebibytes() + 2.mebibytes()).to_string(), "5MiB");
assert_eq!((7.gigabytes() + 58.mebibytes() + 3.kilobytes()).to_string(), "7.06GB");
assert_eq!((7.gibibytes() + 920.mebibytes()).to_string(), "7.90GiB");
assert_eq!(7231.kilobytes().to_string(), "6.90MiB");

assert_eq!(format!("{:.0}", 7.gibibytes() + 920.mebibytes()), "8GiB");
assert_eq!(format!("{:.1}", 7.gibibytes() + 920.mebibytes()), "7.9GiB");
assert_eq!(format!("{:.2}", 7.gibibytes() + 920.mebibytes()), "7.90GiB");
assert_eq!(format!("{:.3}", 7.gibibytes() + 920.mebibytes()), "7.898GiB");
assert_eq!(format!("{:.4}", 7.gibibytes() + 920.mebibytes()), "7.8984GiB");
assert_eq!(format!("{:.4}", 7231.kilobytes()), "6.8960MiB");
assert_eq!(format!("{:.0}", 7231.kilobytes()), "7MiB");
assert_eq!(format!("{:.2}", 999.kilobytes() + 990.bytes()), "976.55KiB");
assert_eq!(format!("{:.0}", 999.kilobytes() + 990.bytes()), "1MB");

assert_eq!(format!("{:04.2}", 999.kilobytes() + 990.bytes()), "0976.55KiB");
assert_eq!(format!("{:02.0}", 999.kilobytes() + 990.bytes()), "01MB");
assert_eq!(format!("{:04.0}", 999.kilobytes() + 990.bytes()), "0001MB");

Formats the status code, including the canonical reason.

Example

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

Formats the ContentType as an HTTP Content-Type value.

Example
use rocket::http::ContentType;

let ct = format!("{}", ContentType::JSON);
assert_eq!(ct, "application/json");

Formats the cookie self as a Set-Cookie header value.

Does not percent-encode any values. To percent-encode, use Cookie::encoded().

Example
use cookie::Cookie;

let mut cookie = Cookie::build("foo", "bar")
    .path("/")
    .finish();

assert_eq!(&cookie.to_string(), "foo=bar; Path=/");

Implementors

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