On this page
  1. Display
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

Display

Prev: error_and_fmt | Up: error_and_fmt | Next: `Error`
Rel: error_and_fmt, rust/builtins
Ref: Rust std — Display
Tags: #public #prog #rust #stdlib #error_and_fmt #tier/core


Signature & Overview

trait Display { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result; }

User-facing formatting ({}); implementing Display automatically implements .to_string().

Notes & Pitfalls

Example

use std::fmt;

struct Version(u8, u8, u8);

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "v{}.{}.{}", self.0, self.1, self.2)
    }
}

fn main() {
    assert_eq!(Version(1, 2, 0).to_string(), "v1.2.0");
}

Linked from

Related notes