trait

Display: user-facing text

Implement a deliberate readable representation. Propagate errors from the formatter; do not invent formatting errors for invalid domain values. Expected stdout (not executed): 12 m Common mistake: Assuming deriving Debug also provides Display.

Formal Syntax & Structure

rust syntaxrust
impl std::fmt::Display for T { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { ... } }

Working Example

Example Snippetrust
fn main() {    use std::fmt;    struct Distance(u32);    impl fmt::Display for Distance {        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{} m", self.0) }    }    println!("{}", Distance(12));}
Rust Reference