// A `Display` impl becomes a proc returning the string the formatter is // written with, since the observable result of `{}` is exactly those bytes. use core::fmt; #[derive(Debug, PartialEq)] pub enum Error { InvalidEncoding, InvalidLength, } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Error::InvalidEncoding => f.write_str("invalid Base16 encoding"), Error::InvalidLength => f.write_str("invalid Base16 length"), } } } // A marker trait with no items: we do not model trait resolution, so it // generates nothing. impl core::error::Error for Error {} 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) } } impl Point { fn sum(&self) -> i32 { self.x + self.y } } fn main() { println!("{}", Error::InvalidEncoding); println!("{}", Error::InvalidLength); println!("{:?}", Error::InvalidLength); let p = Point { x: 3, y: -4 }; println!("{} {}", p, p.sum()); }