Trait core::fmt::Display

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

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

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

Implementors

impl<T: ?Sized + Display> Display for ThinBox<T>

impl<T: Display + ?Sized, A: Allocator> Display for Box<T, A>

impl<B: ?Sized> Display for Cow<'_, B>where
    B: Display + ToOwned<Owned: Display>,

impl<'a, K: Debug + Ord, V: Debug, A: Allocator + Clone> Display for OccupiedError<'a, K, V, A>

impl Display for NulError

impl<T: ?Sized + Display> Display for Rc<T>

impl Display for String

impl<T: ?Sized + Display> Display for Arc<T>

impl Display for LexError

impl Display for Group

impl Display for Punct

impl Display for Ident

impl Display for Literal

impl<'a, K: Debug, V: Debug> Display for OccupiedError<'a, K, V>

impl Display for VarError

impl<E> Display for Report<E>where
    E: Error,

impl<W> Display for IntoInnerError<W>

impl Display for Error

impl Display for IpAddr

impl Display for Ipv4Addr

impl Display for Ipv6Addr

impl Display for Display<'_>

impl<T> Display for SendError<T>

impl<T> Display for TrySendError<T>

impl<T: ?Sized + Display> Display for MutexGuard<'_, T>

impl<T> Display for PoisonError<T>

impl<T> Display for TryLockError<T>

impl<T: ?Sized + Display> Display for RwLockReadGuard<'_, T>

impl<T: ?Sized + Display> Display for RwLockWriteGuard<'_, T>

impl Display for TestName