Using the format! macro
There is an additional way to combine strings, which can also be used to combine them with other data types, such as numbers.
How to do it...
- In the
src/bin
folder, create a file calledformat.rs
- Add the following code and run it with
cargo run --bin format
1 fn main() { 2 let colour = "red"; 3 // The '{}' it the formatted string gets replaced by the parameter 4 let favourite = format!("My favourite colour is {}", colour); 5 println!("{}", favourite); 6 7 // You can add multiple parameters, which will be 8 // put in place one after another 9 let hello = "hello "; 10 let world = "world!"; 11 let hello_world = format!("{}{}", hello, world); 12 println!("{}", hello_world); // Prints "hello world!" 13 14 // format! can concatenate any data types that 15 // implement the 'Display' trait, such as numbers 16 let favourite_num = format!("My favourite number is {}", 42); 17 println!("{}", favourite_num); // Prints "My favourite number is 42" 18 19 // If you want to include certain parameters multiple times 20 // into the string, you can use positional parameters 21 let duck_duck_goose = format!("{0}, {0}, {0}, {1}!", "duck", "goose"); 22 println!("{}", duck_duck_goose); // Prints "duck, duck, duck, goose!" 23 24 // You can even name your parameters! 25 let introduction = format!( 26 "My name is {surname}, {forename} {surname}", 27 surname="Bond", 28 forename="James" 29 ); 30 println!("{}", introduction) // Prints "My name is Bond, James Bond" 31 }
How it works...
The format!
macro combines strings by accepting a format string filled with formatting parameters (example, {}
, {0}
, or {foo}
) and a list of arguments, which are then inserted into the placeholders.
We are now going to show this on the example in line [16]:
format!("My favourite number is {}", 42);
Let's break down the preceding line of code:
"My favourite number is {}"
is the format string{}
is the formatting parameter42
is the argument
As demonstrated, format!
works not only with strings, but also with numbers. In fact, it works with all structs
that implement the Display
trait. This means that, by providing such an implementation by yourself, you can easily make your own data structures printable however you want.
By default, format!
replaces one parameter after another. If you want to override this behavior, you can use positional parameters like {0}
[21]. With the knowledge that the positions are zero-indexed, the behavior here is pretty straightforward, {0}
gets replaced by the first argument, {1}
gets replaced by the second, and so on.
At times, this can become a bit unwieldy when using a lot of parameters. For this purpose, you can use named arguments [26], just like in Python. Keep in mind that all of your unnamed arguments have to be placed before your named ones. For example, the following is invalid:
format!("{message} {}", message="Hello there,", "friendo")
It should be rewritten as:
format!("{message} {}", "friendo", message="Hello there,") // Returns "hello there, friendo"
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!