3 ms·
> Also to escape these curly braces, just put two of them in front of eachother So that would be "Hello {{x}}!".
by SkeuomorphicBee 5y ago
> Also to escape these curly braces, just put two of them in front of eachother
So that would be "Hello {{x}}!".
- georgyo 5y agoIs this feature was introduced in 1.58, does that mean strings in 1.57 didn't need the extra curly braces? 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
- deleted 5y ago[deleted]
- lights0123 5y agoNo. That was invalid syntax previously.
- georgyo 5y agoAt this point I should research it myself, but I'm on mobile currently. 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.
- sodality2 5y ago> I doubt you would escape it with {{ in that release You would. Brackets were still escaping in that release because you would of course still do `println!("{}", x);`
- jeroenhd 5y agoIn previous Rust versions, you can't print {0} by printing `{x}`. Adding braces around variables turns them into named variables. For example: 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}}"); }
- estebank 5y agoThe last bit of information that GP might be missing is that 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.
- lmkg 5y agoYou might be missing the fact that this applies only to format strings, not strings in general. The interpolation into curly brackets is done by the format! (or println!) macros, not a language feature of strings in general.
- paavohtl 5y agoRust has a pretty strict backwards compatibility policy. Generally speaking breaking changes are not allowed, unless they fix a specific vulnerability or other critical issue. Language-level breaking changes can be done with the Edition mechanism. Three editions have been released so far, with three years between each of them. 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.