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

Custom Formatting

Overview

The std::fmt module allows custom display formatting by implementing the Display and Debug traits.

Code Example

#![allow(unused)]
fn main() {
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, "Point({}, {})", self.x, self.y)
    }
}

let point = Point { x: 10, y: 20 };
println!("Custom formatted point: {}", point);
}

Key Concepts

  • Display: For user-facing output ({})
  • Debug: For programmer-facing output ({:?})
  • Implement traits to control formatting

Learn More