So that would be "Hello {{x}}!".
I imagine this is going to break a lot of existing code. There doesn't even seem to be a way to have {x} mean the same thing in 1.57 and 1.58
Additionally, new releases are frequently tested with a tool called Crater, which basically builds and runs the tests for all publicly available Rust code. This helps massively in upholding backwards compatibility guarantees and in evaluating if a compatibility break is worth it.
But how would you print {x} in 1.57? I doubt you would escape it with {{ in that release. Maybe \{ then? Does that still work in 1.58?
This still smells fishy to me, but I need to dig deeper.
pub fn main() {
let y = 4;
println!("{x}", x=y);
}
To print '{x}', you need to write: pub fn main() {
let x = 4; // unused
println!("{{x}}");
} pub fn main() {
let x = 4; // unused
println!("{x}");
}
would previously be a compile time error (because the format! family of macros perform string interpolation at compile time): error: there is no argument named `x`
--> <source>:3:15
|
3 | println!("{x}");
| ^^^
So no new existing code will stop compiling, some things that were previously compile errors will now work.You would. Brackets were still escaping in that release because you would of course still do `println!("{}", x);`
print!("{}", hello_string)
Or as others have pointed out, you can double-up the braces: print!("Hello {{x}}!")
Then if you want to be cute, you could do something like this: print!("Hello {x}!", x="{x}")
Note that the named parameter syntax isn't new -- what's new is capturing named parameters from a scope that's larger than the format macro: let x = "{x}"
print!("Hello {x}!")
And also note that it's a compile-time error to have an unused format parameter in a string, so that last code example wouldn't have compiled at all in older versions of rust.