The ever-improving error messages of Rust

In my last-but-one post, I mentioned the quality of Rust’s compiler errors. I encountered another compiler error today that I thought was less-than-optimal, I was going to file an issue for it… then I discovered it’s been fixed in a newer version of Rust. Nice!

I’ve been writing a lot of Scala this week, and Scala uses s-prefixed strings for string interpolation. For example:

val name = "Alex"
println(s"Hello, $name")  // Hello, Alex

And so when I came to write some Rust, that s-prefix crept into my strings:

let name = "Alex";
println!(s"Hello, {}!", name);

If you compile that code with the 2018 edition of Rust, this is the error you get:

error: expected `,`, found `"Hello, {}!"`
 --> src/main.rs:3:15
  |
3 |     println!(s"Hello, {}!", name);
  |               ^^^^^^^^^^^^ expected `,`

Although it knows there’s an issue somewhere on this line, it’s highlighting everything except the erroneous prefix. I did work out what I’d done wrong, but not after a bit of head-scratching.

I was going to suggest tweaking Rust’s output to highlight this sort of erroneous prefix – string prefixes are used in lots of languages (including Rust itself), so I’m surely not the first person to make this mistake. The issue template asks you to include a runnable example on play.rust-lang.org, and that’s where I discovered this has already been fixed.

If you compile that code with the 2021 edition of Rust, you get three errors – and the first of them highlights this prefix.

error: prefix `s` is unknown
 --> src/main.rs:3:15
  |
3 |     println!(s"Hello, {}!", name);
  |              ^ unknown prefix
  = note: prefixed identifiers and literals are reserved since Rust 2021

Small improvements like this don’t fundamentally change the language or what it can do, but they do add up to a much more pleasant experience as a programmer.