rust - 如何初始化 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
是否有解决此限制的方法,例如使用无效值初始化数组,然后使用循环填充它?
解决方案
推荐阅读
- c# - 从已知宽度和高度的字节数组计算坐标的 X 和 Y
- collections - 如何在特定索引中添加新项目?
- terraform - 使用 Terraform,我如何编排许多相关/依赖堆栈的部署?
- c++ - DFS - 查找源与目标相同的所有路径
- python - 如何在自定义路径中保存/加载 tensorflow 集线器模块?
- scala - Scala - 遍历特定类型
- r - 在 ggplot2 中手动显示图例
- php - 由于目标机器主动拒绝,无法建立连接。
- python - 如何将两个 JSON 附加到一个 JSON,Python Flask?
- html - 如何在Django2.0中使用html或css添加背景