There's more...

You can combine positional parameters with normal ones, but it's probably not a good idea, as it can quite easily become confusing to look at. The behavior, in this case, is as follows—imagine that format! internally uses a counter to determine which argument is the next to be placed. This counter is increased whenever format! encounters a {} without a position in it. This rule results in the following:

format!("{1} {} {0} {}", "a", "b") // Returns "b a a b"

There are also a ton of extra formatting options if you want to display your data in different formats. {:?} prints the implementation of the Debug trait for the respective argument, often resulting in a more verbose output. {:.*} lets you specify the decimal precision of floating point numbers via the argument, like so:

format!("{:.*}", 2, 1.234567) // Returns "1.23"

For a complete list, visit https://doc.rust-lang.org/std/fmt/.

All of the information in this recipe applies to println! and print! as well, as it is essentially the same macro. The only difference is that println! doesn't return its processed string but instead, well, prints it!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.37.38