News for hackers. Stuff that matters.
Re:Rust 1.58.0 by c0balt on Thursday January 13, 2022 @12:42PM (#29923892)
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.
Re:Rust 1.58.0 by echelon on Thursday January 13, 2022 @05:09PM (#29927628)
Yet they stopped short of allowing arbitrary expressions in string formatting. (Good!)

The Rust team is striking all of the right balances between convenience and safe ergonomics.

Re:Rust 1.58.0 by dataangel on Saturday January 15, 2022 @06:08PM (#29951401)
You can’t even access a member though.
Re:Rust 1.58.0 by edflsafoiewq on Thursday January 13, 2022 @02:43PM (#29925741)
> Format strings can only capture plain identifiers, not arbitrary paths or expressions.

Why?

Re:Rust 1.58.0 by pornel on Thursday January 13, 2022 @02:46PM (#29925792)
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.

Re:Rust 1.58.0 by edflsafoiewq on Thursday January 13, 2022 @04:05PM (#29926843)
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").

Re:Rust 1.58.0 by dataangel on Saturday January 15, 2022 @06:09PM (#29951412)
You can have nested quotes already, arbitrarily many levels deep.
Re:Rust 1.58.0 by epage on Thursday January 13, 2022 @03:19PM (#29926244)
Re:Rust 1.58.0 by hajile on Thursday January 13, 2022 @02:25PM (#29925453)
This is a nice step.

Now they should allow expressions in addition to just names and apply it to all string literals.

Re:Rust 1.58.0 by DrMeepster on Thursday January 13, 2022 @02:34PM (#29925609)
This can never happen with regular string literals. String literals have the type `&'static str` but formatting returns a `String`.
Re:Rust 1.58.0 by pornel on Thursday January 13, 2022 @02:54PM (#29925902)
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.
Re:Rust 1.58.0 by hajile on Thursday January 13, 2022 @03:17PM (#29926216)
In truth, it probably requires something like `f"foo {bar}"`
Re:Rust 1.58.0 by pjmlp on Thursday January 13, 2022 @01:37PM (#29924705)
Nice work! Looks like I have some code updates to do.
Re:Rust 1.58.0 by Shadonototra on Thursday January 13, 2022 @04:17PM (#29926993)
index or format specs are ok, but capturing variables is a bad idea, visually bloats the string, it becomes unreadable
Re:Rust 1.58.0 by programmer_dude on Thursday January 13, 2022 @11:17PM (#29930539)
It's not a bad idea. Think of the variable name as documentation.
Re:Rust 1.58.0 by Shadonototra on Friday January 14, 2022 @01:34PM (#29938238)
i maintain, it is a bad idea

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