Since Rust 1.26, main
can return a Result
: "If main
returns an error, this will exit with an error code, and print out a debug representation of the error".
For instance the following code:
use failure::{err_msg, Error};
use std::fs::File;
fn main() -> Result<(), Error> {
let _ = File::open("bar.txt").map_err(|_| err_msg("test"))?;
Ok(())
}
prints out:
Error: ErrorMessage { msg: "test" }
(debug representation) while I would like it to print out:
Error: test
(display representation).
One way would be to revert main
back to the previous style:
fn main() {
if let Err(e) = File::open("bar.txt").map_err(|_| err_msg("test")) {
eprintln!("Error: {}", e);
std::process::exit(1);
}
}
but is there a simple way to customize the error message while keeping the new Rust 1.26 functionality? I hope I do not need to implement my custom Error
type only for this...
Thanks