为什么这段代码无法编译?
trait TFoo {}
fn foo<T: TFoo>(v: Box<T>) {}
fn bar(v: Box<TFoo>) {
foo(v);
}
fn main() {}
编译错误:
error[E0277]: the trait bound `TFoo: std::marker::Sized` is not satisfied
--> src/main.rs:6:5
|
6 | foo(v);
| ^^^ `TFoo` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `TFoo`
= note: required by `foo`
如果foo
的签名为fn foo(v: Box<TFoo>)
,则此代码会进行编译。为什么Box<T> where T: Trait
不能作为参数传递,但可以传递Box<Trait>
?