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