Rust Lang

Rust String Interpolation

String interpolation refers to a method of combining the variables and literal strings in a single string value.

String interpolation is one of the most common tasks in programming, and each language provides a unique way of injecting the variables into a string value.

We can perform the string interpolation using the format! macro in the Rust programming language. This macro allows us to insert the values into a string at runtime using the curly braces {} as placeholders for the values.

In this tutorial, we will go through the steps of using the format! macro to perform the string interpolation.

Rust Format! Macro

In Rust, the format! macro allows us to perform the string interpolation to combine the variables and literal strings.

The macro takes a format string as its first argument which contains the text and optional placeholders for the values of variables.

We denote the placeholders using a pair of curly braces within the format string. The values of the defined variables are then substituted with the defined placeholders at runtime.

An example is as follows:

format!("String, {}!", variable)

 
The placeholder then substitutes with the value of the defined variable at runtime. To better demonstrate this, we can run the following code:

fn main() {
    let name = "Rustacean";
    let greeting = format!("Hello, {}!", name);
    println!("{}", greeting);
}

 
In the previous example, Rust substitutes the variable’s value called “name” in the formatted string.

This prints out “Hello, Rustacean!”

Formatting Values

The format! macro also provides us with several formatting options that we can use to control the method on which the values are interpolated into the strings.

For example, we can tell the macro with the number of decimal places to use when formatting a floating-point value.

An example is as follows:

fn main() {
    let name = "Wesly";
    let age = 20;
    let height = 1.8122;
    let message = format!("Name: {}\nAge: {}\nHeight: {:.2}m", name, age, height);
    println!("{}", message);
}

 
In the previous example, we use the format! macro to create a string including the values of the name, age, and height variables.

Using the format specifier {:2}, we tell the Rust compiler that the value of the height value should be formatted to two decimal places.

The resulting output is as follows:

Name: Wesly
Age: 20
Height: 1.81m

 
As you can see, the macro truncates the value to the specified decimal places.

Named Arguments

We can also work with positional arguments using the format! macro. The named arguments allow us to specify the values that are interpolated into a string using the corresponding variable name.

An example is demonstrated in the following:

fn main() {
    let person = ("Wesley", 20, 1.8223);
    let message = format!("Name: {name}\nAge: {age}\nHeight: {height:.2}m",
        name=person.0, age=person.1, height=person.2);
    println!("{}", message);
}

 
In the previous code, we use the named arguments to interpolate the tuple’s values into the string. Then, we use the values of the tuple using the name=value syntax.

Escape Sequences

Sometimes, we may need to include the curly braces as part of the string values and force Rust not to treat them as placeholder values.

In such a case, we can escape the curly braces by doubling them as follows:

fn main() {
    let message = format!("The value of x is {{}}");
    println!("{}", message);
}

 
Output:

The value of x is {}

 
The previous example uses the double method to escape the curly braces and prevent them from being treated as placeholders.

Conclusion

String interpolation is a powerful feature that allows us to combine the variables and literal strings in a single string value. As you discovered in this tutorial, you can use the format! macro in the Rust programming language to perform the string interpolation.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list