std::fmt::Display for user-facing string representations
Marcus Chen
Jan 2026
1 tab
use std::fmt;
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)
}
}
fn main() {
let p = Point { x: 3, y: 4 };
println!("Point: {}", p);
}
1 file · rust
Explain with highlit
Implementing Display lets your types be formatted with {} in format strings. It's for user-facing output, unlike Debug which is for developers. I implement Display for error types, domain models, and anything that might be printed. The trait requires a fmt method that writes to a formatter. For simple cases, use write!(f, "...", args). For complex formatting, build the string incrementally. Display is also required for converting to strings via .to_string(). I derive or implement it on most public types to improve ergonomics. It's a small effort that makes your API feel polished.