Egor's blog

By Egor, history, 3 years ago, In English

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!.

  • Vote: I like it
  • +107
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Nice to hear!

»
3 years ago, # |
  Vote: I like it -22 Vote: I do not like it

Waited for it!

»
3 years ago, # |
  Vote: I like it +29 Vote: I do not like it

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.

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

I wish we can get RUST_BOOTSTRAP=1 in compile command for rust. So we can enjoy the nightly features ;)

»
3 years ago, # |
  Vote: I like it +39 Vote: I do not like it

Try it here