为什么`Box <t>不能将T:Trait`作为参数传递?

时间:2017-11-06 16:30:06

标签: rust

为什么这段代码无法编译?

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>

0 个答案:

没有答案