Reputation: 86095
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?
Upvotes: 90
Views: 55403
Reputation: 11946
No it does not. While Rust 1.58 added "captured identifiers in format strings", it's not string interpolation. Instead, it lets you use the current scope as the keyword arguments to the println!
macro -- so it's a shortcut to passing those named arguments:
let name = "eonil";
// These two are equivalent:
println!("Hi {name}!");
println!("Hi {name}!", name = name);
This answer explains it in depth.
Since it's only syntatic sugar for formatting macros, it doesn't work like general string interpolation:
let revision = 34343;
let output = Command::new("TortoiseProc.exe")
.arg("/command:log")
.arg("/startrev:{revision}")
// launches uninterpolated "TortoiseProc.exe /command:log /startrev:{revision}"
let output = Command::new("TortoiseProc.exe")
.arg("/command:log")
.arg(format!("/startrev:{revision}")) // using the macro
// launches intended "TortoiseProc.exe /command:log /startrev:34343"
Upvotes: 1
Reputation: 86095
This is available since Rust 1.58! Here's the 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}.");
RFC 2795 is the original proposal.
Upvotes: 102
Reputation: 11756
As of Rust 1.58, you can take advantage of captured identifiers in format strings. That lets you do stuff like this:
let msg = "here";
let s = format!("Abc {msg}");
println!("Hi t{msg}"); // prints "Hi there"
println!("{s}"); // prints "Abc here"
This feature can be seen as a subset of string interpolation. Expressions like format!("Abc {a+b}")
are not supported and it's unclear whether such a feature will ever be added. There have also been discussions on adding a subset of expressions for dotted paths, as in format!("Abc {foo.bar}")
.
Also note that the Rust 2021 Edition makes room for future additions to the language, such as f"hello {name}"
, which would allow for a much more concise string interpolation, comparable to most other modern languages.
Upvotes: 48
Reputation: 213628
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.
Upvotes: 82