如何在 Rust 中使用 BigInt 或 BigUint 生成一系列值?
How can I make a range of values using BigInt or BigUint in Rust?
我想遍历具有 BigUint
类型的值范围(来自 num
板条箱)。
我该怎么做?
我试过了
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`
num
板条箱似乎还不支持,因为 unstability of Step
trait 。
您可以使用 num-iter
crate 及其范围函数。
use num::BigUint;
fn main() {
for i in num_iter::range_inclusive(BigUint::from(0u64), BigUint::from(2u64)) {
println!("{}", i);
}
}
我想遍历具有 BigUint
类型的值范围(来自 num
板条箱)。
我该怎么做?
我试过了
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`
num
板条箱似乎还不支持,因为 unstability of Step
trait 。
您可以使用 num-iter
crate 及其范围函数。
use num::BigUint;
fn main() {
for i in num_iter::range_inclusive(BigUint::from(0u64), BigUint::from(2u64)) {
println!("{}", i);
}
}