Formatted print

Printing is handled by a series of macros defined in std::fmt
some of which include:

  • format!: write formatted text to String
  • print!: same as format! but the text is printed to the console (io::stdout).
  • println!: same as print! but a newline is appended.
  • eprint!: same as format! but the text is printed to the standard error (io::stderr).
  • eprintln!: sames as eprint!but a newline is appended.

All parse text in the same fashion. A plus is that the formatting correctness will
be checked at compile time.

  1. fn main() {
  2. // In general, the `{}` will be automatically replaced with any
  3. // arguments. These will be stringified.
  4. println!("{} days", 31);
  5. // Without a suffix, 31 becomes an i32. You can change what type 31 is,
  6. // with a suffix.
  7. // There are various optional patterns this works with. Positional
  8. // arguments can be used.
  9. println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
  10. // As can named arguments.
  11. println!("{subject} {verb} {object}",
  12. object="the lazy dog",
  13. subject="the quick brown fox",
  14. verb="jumps over");
  15. // Special formatting can be specified after a `:`.
  16. println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
  17. // You can right-align text with a specified width. This will output
  18. // " 1". 5 white spaces and a "1".
  19. println!("{number:>width$}", number=1, width=6);
  20. // You can pad numbers with extra zeroes. This will output "000001".
  21. println!("{number:>0width$}", number=1, width=6);
  22. // It will even check to make sure the correct number of arguments are
  23. // used.
  24. println!("My name is {0}, {1} {0}", "Bond");
  25. // FIXME ^ Add the missing argument: "James"
  26. // Create a structure which contains an `i32`. Name it `Structure`.
  27. #[allow(dead_code)]
  28. struct Structure(i32);
  29. // However, custom types such as this structure require more complicated
  30. // handling. This will not work.
  31. println!("This struct `{}` won't print...", Structure(3));
  32. // FIXME ^ Comment out this line.
  33. }

std::fmt contains many traits which govern the display
of text. The base form of two important ones are listed below:

  • fmt::Debug: Uses the {:?} marker. Format text for debugging purposes.
  • fmt::Display: Uses the {} marker. Format text in a more elegant, user
    friendly fashion.

Here, fmt::Display was used because the std library provides implementations
for these types. To print text for custom types, more steps are required.

Activities

  • Fix the two issues in the above code (see FIXME) so that it runs without
    error.
  • Add a println! macro that prints: Pi is roughly 3.142 by controlling
    the number of decimal places shown. For the purposes of this exercise,
    use let pi = 3.141592 as an estimate for Pi. (Hint: you may need to
    check the std::fmt documentation for setting the number of
    decimals to display)

See also

std::fmt, macros, struct,
and traits