首页 > 解决方案 > 如何初始化 Vec 数组?

问题描述

我想初始化(可变长度)向量的(固定长度)数组,例如:

let array_of_vec: [Vec<i32>; 10] = [Vec::new(); 10];

但我得到:

error[E0277]: the trait bound `std::vec::Vec<i32>: std::marker::Copy` is not satisfied
  |
2 |     let array_of_vec: [Vec<i32>; 10] = [Vec::new(); 10];
  |                                        ^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::vec::Vec<i32>`
  |
  = note: the `Copy` trait is required because the repeated element will be copied

是否有解决此限制的方法,例如使用无效值初始化数组,然后使用循环填充它?

标签: rust

解决方案


推荐阅读