Rust by Example | страница 3
>println!("{subject} {verb} {object}",
>object="the lazy dog",
>subject="the quick brown fox",
>verb="jumps over");
>// Special formatting can be specified after a `:`.
>println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
>// You can right-align text with a specified width. This will output
>// " 1". 5 white spaces and a "1".
>println!("{number:>width$}", number=1, width=6);
>// You can pad numbers with extra zeroes. This will output "000001".
>println!("{number:>0width$}", number=1, width=6);
>// Rust even checks to make sure the correct number of arguments are
>// used.
>println!("My name is {0}, {1} {0}", "Bond");
>// FIXME ^ Add the missing argument: "James"
>// Create a structure named `Structure` which contains an `i32`.
>#[allow(dead_code)]
>struct Structure(i32);
>// However, custom types such as this structure require more complicated
>// handling. This will not work.
>println!("This struct `{}` won't print...", Structure(3));
>// FIXME ^ Comment out this line.
>}
>הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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, we used fmt::Display because the std library provides implementations for these types. To print text for custom types, more steps are required.
Implementing the fmt::Display trait automatically implements the ToString trait which allows us to convert the type to String.
• 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)