One thing you may be missing is that the `Result::Err` in your return type must implement `From` for the `Result::Err` type where you're using the `?` operator. That's already covered if those `Err` types are identical (`From<T> for T`), otherwise you need to implement the conversion.
impl From<QuestionError> for ReturnError {
fn from(error: QuestionError) -> ReturnError {
// TODO
unimplemented!()
}
}Oh yeah, that's likely it. Thanks!
It it safe to just put a noop in there? What's From-ness? Is QuestionError defined in std? Where do I read more?
EDIT: oh I see, I didn't read carefully. I just need to provide a conversion. Your example is very clear.
It's how you explicitly cast from one type to another. To use the ? operator with a custom error type, it needs to be converted to the std error type.
Re:Things I Enjoy in Rust: Error Handling
by steveklabnik on Monday April 22, 2019 @04:44PM
(#19722795)
It needs to be converted to whatever error type the function returns. That can be anything.
Also, if you use Box<Error> or failure[1]'s Error type you can get around implementing most of the conversion Traits, but you lose the ability to tell which errors can happen by just looking at the type signature and doing exhaustive matching on them.
The ? operator is tied to std::ops::Try, and std::convert::From. https://doc.rust-lang.org/std/ops/trait.Try.html https://doc.rust-lang.org/std/convert/trait.From.html
Oh, hey, this was great (the link to the 'From' trait). "enum CliError" -- this example was crystal clear and answered a lot of my questions.
If you can post your code, we can help you out. One requirement, is that your function returns a Result<Ok, Err>. The Err needs to implement the same type for all the type returned.
This means you can use the "?" operator for functions that you did not create (libraries) but you'll also have to convert their Error type to your Error type.
It's worth reading through the doc page. It's not not too long: https://doc.rust-lang.org/stable/book/ch09-02-recoverable-er...
Re:Things I Enjoy in Rust: Error Handling
by wyldfire on Tuesday April 23, 2019 @10:41AM
(#19729065)
I had read that doc page but IMO it's not very clear about how you should use Result types. It's great for showing how you might want to use the question mark with libstd but IMO less clear about writing your own functions and Result<> types (though apparently the most interesting constraint seems to be regarding your Result's E type).
Someone elsewhere in this thread linked to an example of `impl From<io::Error>` and IMO that's a clear, explicit example of how to do it.
Unfortunately I haven't been able to get the question mark operator to work yet with my code, but it looks appealing. It's not 100% obvious to me what qualities my Result type needs in order to be able to make it work. Or whether it's bound in practice to the Result type(s) defined in std. I'm sure those details are all in the docs somewhere but I spent the time to look yet.