I love Rust language and feel sorry for this criticism. But small things are important.
I love Rust language and feel sorry for this criticism. But small things are important.
The language has actually thought of this case -- it turns out you can map lower-level errors into your higher-level error type by implementing the `From<E>` trait. E.g.:
use std::io;
pub enum MyErr { Io(io::Error), Custom(String) }
impl From<io::Error> for MyErr {
fn from(err: io::Error) -> MyErr { MyErr::Io(err) }
}
fn do_stuff() -> Result<(), MyErr> {
let f = try!(File::open(...));
// ...
}I still sometimes make mistakes reading rust code with the ? at the end of the line, but it's just a question of what type is it and does the line return. Both of which are handled by the compiler in the end, so you get a little bit of extra knowledge that even if you misread the line of code, there's often limits to how much damage you can cause.
Maybe you already know this, but if not (and for others that don't), try it immediately. You'll be glad you did.
And error-chain exists if you want a more fluid system of errors.
Or you can implement a conversion from the underlying error to your custom error type[1], and 'try'/'?' will just work.
[1]https://doc.rust-lang.org/book/error-handling.html#composing...