Closed
Description
#[derive(Default)]
struct Foo<T> {
bar: Box<[T]>,
}
impl<T> Foo<T> {
fn new() -> Self {
Self::default()
}
}
This gives me the following error:
error[E0599]: no function or associated item named `default` found for type `Foo<T>` in the current scope
--> src/main.rs:8:9
|
2 | struct Foo<T> {
| ------------- function or associated item `default` not found for this
...
8 | Self::default()
| ^^^^^^^^^^^^^ function or associated item not found in `Foo<T>`
|
= note: the method `default` exists but the following trait bounds were not satisfied:
`Foo<T> : std::default::Default`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `default`, perhaps you need to implement it:
candidate #1: `std::default::Default`
If I change Self::default()
to Foo::default()
, I get the following error, which is much more straightforward:
error[E0277]: the trait bound `T: std::default::Default` is not satisfied
--> src/main.rs:8:9
|
8 | Foo::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 `Foo<T>`
= note: required by `std::default::Default::default`
Both can be fixed by adding a trait bound to the impl
block (impl<T: Default> Foo<T> {...}
).