为什么特质绑定默认不满意?

时间:2018-05-27 18:23:11

标签: rust

我尝试使用支持默认情况下创建实例的容器创建结构。这是一个最小的例子:

#[derive(Default)]
struct Container<T> {
    values: Vec<T>,
}

impl<T> Container<T> {
    fn new() -> Self {
        Default::default()
    }
}

fn main() {}

为什么此代码无法使用

进行编译
error[E0277]: the trait bound `T: std::default::Default` is not satisfied
 --> src/main.rs:8:9
  |
8 |         Default::default()
  |         ^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `T`
  |
  = help: consider adding a `where T: std::default::Default` bound
  = note: required because of the requirements on the impl of `std::default::Default` for `Container<T>`
  = note: required by `std::default::Default::default`

我能理解我是否使用了像Vec :: resize_default()这样的东西,但这里需要什么呢?

0 个答案:

没有答案