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

thousands crate for struct with Display (commafy)

  • thousands
  • commafy
use thousands::Separable;

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

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

fn main() {
    assert_eq!(12345.separate_with_commas(), "12,345");

    assert_eq!("12345".separate_with_commas(), "12,345");

    let p = Point { x: 1234, y: 4567 };
    println!("{p}");
    println!("{}", p.separate_with_commas());
}
x1234 4567x
x1,234 4567x