Being able to use formatting patterns similar to python sounds really awesome. It makes formatting with long formatting strings really a lot more expressive and more readable.
Perhaps something could be done with compile-time code execution, but the bigger problem is that it wouldn't be backwards-compatible. Currently "{foo}" is a perfectly valid string that doesn't evaluate anything.
Because arbitrary expressions open a pandora box that complicates everything touching them.
For example, expressions can be strings, so now you have strings inside strings, and the strings may even contain further levels of nested formatting strings. This would make correct tokenization of Rust quite difficult, and out of reach of most syntax highlighting tools.
So the next question is why not allow only some "simple" expressions. It would require teaching users which expressions are allowed and which aren't. The rule that only a single identifier is allowed is both simple to parse, and simple to teach.
It's hardly a rare feature. Everything from C# to JS has it these days, so I think "out of reach of most syntax highlighting tools" might be overstating things a bit. It can't be that hard.
You don't have to allow nested strings either; Python's f-strings don't, they always parse as a regular string too (well, you can use a 'single-quoted string' inside a "double-quoted f-string").
17 comments
[ 3.3 ms ] story [ 49.7 ms ] threadThe Rust team is striking all of the right balances between convenience and safe ergonomics.
Now they should allow expressions in addition to just names and apply it to all string literals.
Why?
For example, expressions can be strings, so now you have strings inside strings, and the strings may even contain further levels of nested formatting strings. This would make correct tokenization of Rust quite difficult, and out of reach of most syntax highlighting tools.
So the next question is why not allow only some "simple" expressions. It would require teaching users which expressions are allowed and which aren't. The rule that only a single identifier is allowed is both simple to parse, and simple to teach.
You don't have to allow nested strings either; Python's f-strings don't, they always parse as a regular string too (well, you can use a 'single-quoted string' inside a "double-quoted f-string").
i used to use it with C#, so i talk with experience
but what ever.. this is rust we are talking about, it's sad that they decided to follow Scala and bloat their language