I'm trying to understand why map
triggers a compilation error where a match
expression doesn't.
I have:
use std::boxed::Box;
use std::io::Error;
trait MyTrait {
fn foo(&self);
}
struct MyStruct { }
impl MyTrait for MyStruct {
fn foo(&self) { }
}
I'd like to write a function that transforms an instance of MyStruct
to an instance of Box<MyTrait>
.
This compiles:
fn bar() -> Result<Box<MyTrait>, Error> {
let x = Ok(MyStruct {});
match x {
Ok(y) => Ok(Box::new(y)),
Err(err) => Err(err),
}
}
While this:
fn bar() -> Result<Box<MyTrait>, Error> {
let x = Ok(MyStruct {});
x.map(|y| Box::new(y))
}
Gives an error:
error[E0308]: mismatched types
--> src\main.rs:16:2
|
14 | fn bar() -> Result<Box<MyTrait>, Error> {
| --------------------------- expected `std::result::Result<std::boxed::Box<MyTrait + 'static>, std::io::Error>` because of return type
15 | let x = Ok(MyStruct {});
16 | x.map(|y| Box::new(y))
| ^^^^^^^^^^^^^^^^^^^^^^ expected trait MyTrait, found struct `MyStruct`
|
= note: expected type `std::result::Result<std::boxed::Box<MyTrait + 'static>, std::io::Error>`
found type `std::result::Result<std::boxed::Box<MyStruct>, _>`
What's the difference between the two bar()
implementations?