### Egor's blog

By Egor, history, 8 days ago,

Rust 1.58 was just released (rust releases new version every 6 weeks). The only feature relevant to competitive programming is this:

#### Captured identifiers in format strings

Format strings can now capture arguments simply by writing {ident} in the string. Formats have long accepted positional arguments (optionally by index) and named arguments, for example:

println!("Hello, {}!", get_person());                // implicit position
println!("Hello, {0}!", get_person());               // explicit index
println!("Hello, {person}!", person = get_person()); // named


Now named arguments can also be captured from the surrounding scope, like:

let person = get_person();
// ...
println!("Hello, {person}!"); // captures the local person


This may also be used in formatting parameters:

let (width, precision) = get_format();
for (name, score) in get_scores() {
println!("{name}: {score:width$.precision$}");
}


Format strings can only capture plain identifiers, not arbitrary paths or expressions. For more complicated arguments, either assign them to a local name first, or use the older name = expression style of formatting arguments.

This feature works in all macros accepting format strings. However, one corner case is the panic! macro in 2015 and 2018 editions, where panic!("{ident}") is still treated as an unformatted string -- the compiler will warn about this not having the intended effect. Due to the 2021 edition's update of panic macros for improved consistency, this works as expected in 2021 panic!.

• +107

 » 8 days ago, # |   0 Nice to hear!
 » 8 days ago, # |   -22 Waited for it!
 » 8 days ago, # |   +29 I would surely love to see something like "Rust Heroes" contest on Codeforces (similar to "Kotlin Heroes"). And I wonder about how many participants would it be able to realistically attract.
 » 7 days ago, # |   +3 I wish we can get RUST_BOOTSTRAP=1 in compile command for rust. So we can enjoy the nightly features ;)
 » 7 days ago, # |   +39 Try it here
•  » » 6 days ago, # ^ |   +3 Thanks