5

I am having a weird problem:

trait A {}
trait B : A {}

struct MyStruct {}
impl A for MyStruct {}
impl B for MyStruct {}

fn fun_b() -> Box<B> {
    Box::new(MyStruct{})
}

fn fun_a() -> Box<A> {
    /*
    error: mismatched types [E0308]
    note: expected type `Box<A + 'static>`
    note:    found type `Box<B + 'static>`
    */
    fun_b()
}

fn main() {
    fun_a();
    fun_b();
}

It compiles if I replace fun_a with:

fn fun_a() -> Box<A> {
    Box::new(MyStruct{})
}

(which does exactly the same as fun_b)

Do I need to explicitly cast here? Why, and more importantly how?

gan_
  • 656
  • 5
  • 15

0 Answers0