I'm trying to create a struct with a container that supports instance creation with default. Here's a minimal example:
#[derive(Default)]
struct Container<T> {
values: Vec<T>,
}
impl<T> Container<T> {
fn new() -> Self {
Default::default()
}
}
fn main() {}
Why does this code fail to compile with
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`
I could understand if I used something like Vec::resize_default() but for what is it needed here?