55

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?

eonil
  • 79,444
  • 75
  • 307
  • 502

3 Answers3

65

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.

Dietrich Epp
  • 194,726
  • 35
  • 326
  • 406
  • 14
    It seems the discussion didn't continue much. Sad. – eonil Jan 24 '14 at 05:10
  • @ChrisMorgan: That's just background information. The background information hasn't changed in the past 23 months, unless you have something you'd like to share. – Dietrich Epp Jan 24 '14 at 06:26
  • 10
    You can also use `format!("BBB {name}", name = a)`. (It would probably actually be possible to use the same parsing & macro infrastructure as `format` uses to achieve `inline_fmt!("BBB {a}")`, although hygiene may require some tricks to work around.) – huon Jan 24 '14 at 11:09
  • @DietrichEpp: in 23 months a lot has changed about the language; things that were considered infeasible or inappropriate then may not be any more. For example, back then printf-formatting was the way things were done while now it is the new style formatting. There is also a much larger community now. These all change the dynamics considerably. – Chris Morgan Jan 24 '14 at 23:35
  • @ChrisMorgan: We're talking about different things here. I'm talking about the background information: "How do other languages implement string formatting, and why would we choose one syntax over the other?" That hasn't changed. – Dietrich Epp Jan 24 '14 at 23:42
  • 9
    Both Python and C# have since added string interpolation. Python: `f'{a} times {b} is {a * b}.'`, C#: `$"{a} times {b} is {a * b}"` – Ghost4Man Dec 20 '19 at 14:57
  • 3
    Here is a more recent discussion on this topic: https://github.com/rust-lang/rfcs/issues/1250. This answer currently doesn’t feel relevant anymore, since it justifies Rust’s string formatting as being about portability to other languages, which is no longer true – Python, C#, JS, etc, all have syntaxes closer to Ruby than the Rust one. – Thibaud Colas Jan 27 '20 at 07:14
  • This doesn't work on constants (at the time of this writing). – code_dredd Aug 10 '20 at 23:59
45

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.

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
eonil
  • 79,444
  • 75
  • 307
  • 502
  • IMO coming from JS that RFC is . The mentioned full-interpolation syntax `{( )}` has awful readability, especially compared to `${ }`, and the author has a mysteriously strong bias against supporting interpolation of arbitrary expressions. Instead they'd rather wrestle with the ways string concatenation throws a wrench into the macro formatting system. Having a string literal syntax where arbitrary expressions can be embedded in `${ }` is just so much simpler. – Andy Nov 30 '21 at 06:42
  • 11
    It was release in Rust version 1.58.0 – Rodrigo Stuchi Jan 13 '22 at 18:46
23

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}");
println!("{s}");

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.

at54321
  • 4,634
  • 8
  • 23