Блог пользователя Egor

Автор Egor, история, 3 года назад, По-английски

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
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Nice to hear!

»
3 года назад, # |
  Проголосовать: нравится -22 Проголосовать: не нравится

Waited for it!

»
3 года назад, # |
  Проголосовать: нравится +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.

»
3 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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

»
3 года назад, # |
  Проголосовать: нравится +39 Проголосовать: не нравится

Try it here