main.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. fn formatted_stuff() {
  2. // string formatting
  3. // automatically casts to string (I assume that's a trait?)
  4. println!("{} days", 31);
  5. // Positional arguments
  6. // it should check that all are there at compile time
  7. println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
  8. // it does check. this form uses macros (makes sense, really)
  9. // Something like "error: invalid reference to argument `2` (there are 2 arguments)"
  10. // named argments, too
  11. println!("{subject} {verb} {object}",
  12. object="the lazy dog",
  13. subject="the quick brown fox",
  14. verb="jumps over");
  15. // formatting flags are announced by ':'
  16. // I'm guessing 'b' means 'format as a binary number'
  17. println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
  18. // "width", with padding
  19. println!("{number:>width$}", number=1, width=6);
  20. // and zero padding
  21. println!("{number:>0width$}", number=1, width=6);
  22. // this doesn't work for all characters -- just 0
  23. // the '$' means "look for the width in the named argument 'width'
  24. // can also use a positional argument
  25. println!("{number:>0$}", 6, number=1);
  26. // or an inline width
  27. println!("{number:>6}", number=1);
  28. // see https://doc.rust-lang.org/std/fmt/ for more info
  29. }
  30. fn main() {
  31. formatted_stuff();
  32. }