Alex Garella
9th May 2023
In Rust, working with multiline strings is a common task, especially when it comes to templating or generating code. Although Rust doesn't provide dedicated syntax for multiline string interpolation, you can still achieve this using raw string literals and the format!
macro. In this blog post, we'll walk through how to format a multiline string with interpolation in Rust, complete with code examples.
Rust supports raw string literals, which allow you to create multiline strings without the need to escape special characters. To define a raw string literal, use the r#
prefix and #
postfix. Here's an example:
fn main() {
let multiline_string = r#"This is a
multiline
string."#;
println!("{}", multiline_string);
//Output:
//This is a
//multiline
//string.
}
In this example, we use the r#
prefix to denote a raw string literal. The string contains newlines without any need to escape them. Just remember to add a #
at the end of the string.
format!
Macro for String InterpolationTo perform string interpolation in Rust, you can use the format!
macro. This allows you to include placeholders like {}
inside the string, which will be replaced by the corresponding values. Here's a simple example:
fn main() {
let name = "Alice";
let greeting = format!("Hello, {}", name);
println!("{}", greeting);
// Output: Hello, Alice
}
format!
MacroNow that we know how to create multiline strings with raw string literals and perform string interpolation using the format!
macro, let's combine them to format a multiline string with interpolation:
fn main() {
let name = "John";
let age = 30;
let job = "Software Engineer";
let multiline_string = format!(r#"Hello, my name is {}.
I am {} years old.
I work as a {}."#, name, age, job);
println!("{}", multiline_string);
//Output:
//Hello, my name is John.
//I am 30 years old.
//I work as a Software Engineer.
}
In this example, we use a raw string literal with the r#
prefix for the multiline string. We then use the format!
macro to interpolate the name
, age
, and job
variables into the string. The resulting multiline string is printed to the console with the println!
macro.
If you wish to use string literals with indentation you could consider using the formatdoc!
macro from the indoc crate.
use indoc::formatdoc;
fn main() {
let name = "John";
let age = 30;
let job = "Software Engineer";
let multiline_string = formatdoc!(r#"
{{
"name": {name},
"age": {age},
"job": {job},
}}"#, name = name, age = age, job = job,
);
println!("{}", multiline_string);
//Output:
//{
// "name": John,
// "age": 30,
// "job": Software Engineer,
//}
}
In this example the formatdoc!
macro takes a multiline string literal and un-indents it so the leftmost non-space character is in the first column.
Note that the curly braces {
and }
, inside of the raw string literal in the example above, need to be escaped as {{
and }}
in order to compile.
Rust provides a straightforward way to handle multiline strings and string interpolation through raw string literals and the format!
macro. By combining these features, you can effectively format multiline strings with interpolation in Rust, making it easier to work with complex text and code generation tasks.
We hope this guide has helped you understand the process of formatting multiline strings with interpolation in Rust. Feel free to explore further and use these techniques in your own Rust projects!