They have to be convertible to the type that's being returned. You'd need an implementation of y -> z. It will call the conversion function for you.
use std::fs::File;
enum MyError {
OhNo(String),
}
fn foo() -> Result<File, MyError> {
Ok(File::open("foo.txt")?)
}
With only the above code, we get an error:
the trait bound `MyError: std::convert::From<std::io::Error>` is not satisfied
So by implementing it...
use std::convert::From;
use std::io;
impl From<io::Error> for MyError {
fn from(e: io::Error) -> MyError {
// do some sort of conversion
MyError::OhNo(String::from("some error"))
}
}
Now `?` does the coercion automatically.