Display trait — custom formatting.
Implementation:
1use std::fmt;23struct Point {4 x: f64,5 y: f64,6}78impl fmt::Display for Point {9 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {10 write!(f, "({}, {})", self.x, self.y)11 }12}1314let p = Point { x: 1.0, y: 2.0 };15println!("{}", p); // (1, 2)
Debug trait:
1#[derive(Debug)]2struct Point { x: f64, y: f64 }34println!("{:?}", p); // Point { x: 1.0, y: 2.0 }5println!("{:#?}", p); // Pretty print
Key: Display for user output, Debug for debugging.