Does Rust support Ruby-like string interpolation?

In Ruby I could do this.

aaa = "AAA"
bbb = "BBB #{aaa}"

puts(bbb)

> "BBB AAA"

The point of this syntax is eliminating repetition, and making it to feel like a shell script - great for heavy string manipulation.

Does Rust support this? Or have plan to support this? Or have some feature which can mimic this?


Solution 1:

Rust has string formatting.

fn main() {
    let a = "AAA";
    let b = format!("BBB {}", a);
    println(b);
}
// output: BBB AAA

In the Rust version, there is no additional repetition but you must explicitly call format!() and the inserted values are separated from the string. This is basically the same way that Python and C# developers are used to doing things, and the rationale is that this technique makes it easier to localize code into other languages.

The Rust mailing list has an archived discussion ([rust-dev] Suggestions) in which the different types of string interpolation are discussed.

Solution 2:

RFC 2795 has been accepted, but not yet implemented. The proposed syntax:

let (person, species, name) = ("Charlie Brown", "dog", "Snoopy");

// implicit named argument `person`
print!("Hello {person}");

// implicit named arguments `species` and `name`
format!("The {species}'s name is {name}.");

I hope to see it soon.