Rust 1.58

Revision en1, by Egor, 2022-01-13 22:36:10

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

Tags rust

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Egor 2022-01-13 22:36:10 1631 Initial revision (published)