Formatting

We’ve seen that formatting is specified via a format string:

  • format!("{}", foo) -> "3735928559"
  • format!("0x{:X}", foo) ->
    "0xDEADBEEF"
  • format!("0o{:o}", foo) -> "0o33653337357"

The same variable (foo) can be formatted differently depending on which
argument type is used: X vs o vs unspecified.

This formatting functionality is implemented via traits, and there is one trait
for each argument type. The most common formatting trait is Display, which
handles cases where the argument type is left unspecified: {} for instance.

  1. use std::fmt::{self, Formatter, Display};
  2. struct City {
  3. name: &'static str,
  4. // Latitude
  5. lat: f32,
  6. // Longitude
  7. lon: f32,
  8. }
  9. impl Display for City {
  10. // `f` is a buffer, this method must write the formatted string into it
  11. fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  12. let lat_c = if self.lat >= 0.0 { 'N' } else { 'S' };
  13. let lon_c = if self.lon >= 0.0 { 'E' } else { 'W' };
  14. // `write!` is like `format!`, but it will write the formatted string
  15. // into a buffer (the first argument)
  16. write!(f, "{}: {:.3}°{} {:.3}°{}",
  17. self.name, self.lat.abs(), lat_c, self.lon.abs(), lon_c)
  18. }
  19. }
  20. #[derive(Debug)]
  21. struct Color {
  22. red: u8,
  23. green: u8,
  24. blue: u8,
  25. }
  26. fn main() {
  27. for city in [
  28. City { name: "Dublin", lat: 53.347778, lon: -6.259722 },
  29. City { name: "Oslo", lat: 59.95, lon: 10.75 },
  30. City { name: "Vancouver", lat: 49.25, lon: -123.1 },
  31. ].iter() {
  32. println!("{}", *city);
  33. }
  34. for color in [
  35. Color { red: 128, green: 255, blue: 90 },
  36. Color { red: 0, green: 3, blue: 254 },
  37. Color { red: 0, green: 0, blue: 0 },
  38. ].iter() {
  39. // Switch this to use {} once you've added an implementation
  40. // for fmt::Display
  41. println!("{:?}", *color);
  42. }
  43. }

You can view a full list of formatting traits and their argument
types in the std::fmt documentation.

Activity

Add an implementation of the fmt::Display trait for the Color struct above
so that the output displays as:

  1. RGB (128, 255, 90) 0x80FF5A
  2. RGB (0, 3, 254) 0x0003FE
  3. RGB (0, 0, 0) 0x000000

Two hints if you get stuck:

See also

std::fmt