Please consider the following two functions:
use std::error::Error;
fn foo() -> Result<i32, Box<dyn Error>> {
let x: Result<Result<i32, Box<dyn Error>>, &str> = Err("error");
x?
}
fn bar() -> Result<i32, Box<dyn Error>> {
Err("error")
}
foo()
will compile, but bar()
won’t.
I guess I understand why bar()
doesn’t work: We expect an object implementing the Error
trait in a Box
but we pass a &str
which neither implements Error
nor is it in a Box
.
I wonder why foo()
compiles though. Won’t the ?
operator also try to convert the &str
into a Box<dyn Error>
in this case? Why does it succeed?