rust - 如何在 Rust 中使用 BigInt 和 Bigint 生成一系列值?

标签 rust iterator ownership bignum

我想遍历一系列具有 BigUint 类型的值(来自 num crate )。

我该怎么做?

我试过了

for i in 0..a {...}

其中 a 是(借用的)BigUint 类型。我收到有关不匹配整数类型的错误,所以我改为尝试这样做:

for i in Zero::zero()..a {...}

但是根据 a 是否被借用,我会得到不同的错误。 如果 a 是借用的,那么我会在错误中得到这个:

|    for i in Zero::zero()..(a) {
|             ^^^^^^^^^^ the trait `num::Zero` is not implemented for `&num::BigUint`

如果a不是借来的,那么就是这个错误:

|    for i in Zero::zero()..(a) {
|             ^^^^^^^^^^^^^^^^^ the trait `std::iter::Step` is not implemented for `num::BigUint`

最佳答案

因为 unstability of Step trait num 箱子似乎还不支持它.

你可以做的是使用num-iter crate 及其范围函数。

use num::BigUint;

fn main() {
    for i in num_iter::range_inclusive(BigUint::from(0u64), BigUint::from(2u64)) {
        println!("{}", i);
    }
}

关于rust - 如何在 Rust 中使用 BigInt 和 Bigint 生成一系列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56722409/

相关文章:

rust - 我如何使用 Rust 中的匹配语句拥有的值?

Rust 将 trait 的实现依赖注入(inject)到另一个对象中

linux - 如何使用 `waitpid`等待Rust中的进程?

java - 迭代java集合的性能高效方法是什么?

使用迭代器时 C++ vector 内容未更改

generics - 如何结合 std::str::lines 和 std::io::lines?

使用rust "error: moving out of immutable field"

for-loop - 如何在循环内创建向量的向量?

c++ - 如何安全地销毁一个经常被两个不同线程访问的对象?

rust - 如何在借用不可变值的比赛中进行变异?