Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Other: Print struct (Point)

  • std::fmt::Display
  • Display
use std::fmt;

struct Point {
    x: i32,
    y: i32,
}

impl std::fmt::Display for Point {
    fn fmt(&self, format: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(format, "({}, {})", self.x, self.y)
    }
}

fn main() {
    let pnt = Point { x: 2, y: 3 };

    println!("The point is: {pnt}");
}
The point is: (2, 3)