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?
10 months ago. by John Otieno. String interpolation refers to the process of adding placeholders that reference other values in a string object. The values interpolated into a string are evaluated to their corresponding values.
Beginning with C# 10, you can use string interpolation to initialize a constant string. All expressions used for placeholders must be constant strings. In other words, every interpolation expression must be a string, and it must be a compile time constant.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With