如何通过派生Default
trait来初始化一个默认值为i32
(0
)的整数数组[i32; 100]
?我写了这段代码:
#[derive(Default)]
struct A {
a: i32,
arr: [i32; 100],
}
但是编译器拒绝它,并返回以下错误:
error[E0277]: the trait bound `[i32; 100]: Default` is not satisfied
--> src/main.rs:4:5
|
1 | #[derive(Default)]
| ------- in this derive macro expansion
...
4 | arr: [i32; 100],
| ^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `[i32; 100]`
|
= help: the following other types implement trait `Default`:
[T; 0]
[T; 1]
[T; 2]
[T; 3]
[T; 4]
[T; 5]
[T; 6]
[T; 7]
and 27 others
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)