在我当前的项目中,我正在尝试编写一些可由此最小示例所表示的内容:
#[derive(Default)]
struct A<T> {
field: std::marker::PhantomData<T>
}
struct B;
fn main() {
let a = A::<B> {
..Default::default()
};
}
然而,这段代码无法编译。
error[E0277]: the trait bound `B: std::default::Default` is not satisfied
--> src/main.rs:10:11
|
10 | ..Default::default()
| ^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `B`
|
= note: required because of the requirements on the impl of `std::default::Default` for `A<B>`
= note: required by `std::default::Default::default`
error: aborting due to previous error
对我而言,这有点奇怪,因为Default
是从A
和PhantomData<T>
派生的,那么为什么如果它没有为B
实现就会有影响呢?