I've found myself using .expect("...") to panic with an helpful error message in a lot of places where I don't care about recovering from an error. Example:
let p = "foo.txt";
let f = File::open(p).expect("File not found");
However, I would like to print more information by using a formatted string. This is what I could do:
let f = File::open(p).expect(&format("{} not found", p));
This has two problems:
The
formatcall will be evaluated eagerly.It's unnecessarily verbose.
Ideally, I would like to write:
// pseudocode
let f = File::open(p).expect_fmt("{} not found", p);
But I suppose that's not possible without variadic generic functions and compile-time parsing of strings.
The only alternative I've found is the following:
let f = File::open(p).unwrap_or_else(|_| panic!("{} not found", p));
Which is still a little bit too verbose for my taste.
It's acceptable if the answer uses nightly Rust.