I have the following code:
trait Foo {}
trait Bar: Foo {}
struct Baz {
baz: Box<dyn Foo>,
}
impl Baz {
pub fn new_bar(baz: Box<dyn Bar>) -> Baz {
Baz { baz }
}
}
fn main() {}
This fails to compile because:
error[E0308]: mismatched types
--> foo.rs:14:13
|
14 | baz
| ^^^ expected trait `Foo`, found trait `Bar`
|
= note: expected struct `Box<(dyn Foo + 'static)>`
found struct `Box<(dyn Bar + 'static)>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
However, Bar
is a subtrait(?) of Foo
. Therefore this should typecheck -- why is rustc
complaining, and how can I fix this?