vector - Rust - 大小在运行时定义的向量

标签 vector rust

如何在 Rust 中创建一个大小在运行时定义的数组?

基本上,您如何在 Rust 中转换以下代码:

void f(int n){ return std::vector<int>(n); }

?

这在 rust 中是不可能的:

let n = 15;
let board: [int, ..n];

注意:我看到不可能以简单的方式做到这一点,here , 但我拒绝接受这么简单的事情是不可能的:p

非常感谢!

最佳答案

没关系,我是这样找到的:

let n = 15;      // number of items
let val = 17;    // value to replicate
let v = std::vec::from_elem(val, n);

关于vector - Rust - 大小在运行时定义的向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393508/

相关文章:

c++ - static "global" vector 似乎没有被填充

rust - 当我很少需要可变数据时,如何在多个线程之间共享数据?

c++ - C++98 中的 std::remove_if 用法

C++排序 vector 的指针错误

rust - Rust 语法 `if let [.., last] = self ...` 是什么意思?

rust - Rust 中的替换排列?

image-processing - 如何在 Rust 中将图片转换为纯黑白

naming - 为什么叫它 "Rust"?

R - 如何重新排序行索引号

c++ - 如果 vector 有足够的空间(通过保留创建),std::vector::insert() 是否会使迭代器无效?