Description
I've been working with a lot of people who are either new to Rust or new to its latest features (like the question mark operator) and I'm noticing a lot of confusion around a specific error message.
Suppose you have the following code:
#[derive(Debug)]
struct Error1;
#[derive(Debug)]
struct Error2;
fn foo() -> Result<(), Error1> {
Err(Error1)
}
fn main() -> Result<(), Error2> {
foo()?;
Ok(())
}
This gives you the error:
error[E0277]: the trait bound `Error2: std::convert::From<Error1>` is not satisfied
--> src/main.rs:11:5
|
11 | foo()?;
| ^^^^^^ the trait `std::convert::From<Error1>` is not implemented for `Error2`
|
= note: required by `std::convert::From::from`
However, if you look at the actual code, it isn't obvious where the From
trait is even being used.
The documentation for the From
trait actually does mention this briefly, though most people I encounter don't go as far as looking that up. The error message is pretty daunting if you're new to the language.
I propose that we add something to the error message when we see that they are using the question mark:
help: the question mark operation (`?`) implicitly performs a conversion
on the error value using the `From` trait. If no conversion is
defined, you will see this error message.
This conveys exactly where the From
trait is being used and why they may be running into this issue. You certainly don't have to use this exact wording. Feel free to use whatever fits. :)