我想创建一个向量数组:
fn main() {
let v: [Vec<u8>; 10] = [Vec::new(); 10];
}
但是,编译器给我这个错误:
error[E0277]: the trait bound `std::vec::Vec<u8>: std::marker::Copy` is not satisfied
--> src/main.rs:2:28
|
2 | let v: [Vec<u8>; 10] = [Vec::new(); 10];
| ^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::vec::Vec<u8>`
|
= note: the `Copy` trait is required because the repeated element will be copied
最佳答案
您可以使用 Default
特性来使用默认值初始化数组:
let array: [Vec<u8>; 10] = Default::default();
参见 this playground一个工作示例。
关于arrays - 如何初始化向量数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27393166/