arrays - 如何初始化向量数组?

标签 arrays rust

我想创建一个向量数组:

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/

相关文章:

arrays - 数组与内存数据库

python - 分割给定字节偏移量的 utf-8 编码字符串 (python 2.7)

javascript - 将json多维转换为数组javascript

javascript - Lodash - 使用具有多个属性的meanBy

arrays - 如何创建一个静态字符串数组?

windows - 书中的 FFI 示例在 Windows 下找不到 -lanneclib

c - C中变量定义长度的数组如何存储在内存中?

rust - 运行 `rustc` 检查程序而不生成任何文件

rust - serde skip 属性实际上是否跳过枚举变体?

go - gRPC客户端不占用服务器端流