我遇到了一个奇怪的问题:
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();
}
如果我将fun_a
替换为:
fn fun_a() -> Box<A> {
Box::new(MyStruct{})
}
(与fun_b
完全相同)
我需要在这里明确投射吗?为什么,更重要的是如何?