rust-num / num-bigint

Big integer types for Rust
Apache License 2.0
544 stars 189 forks source link

implement Trait std::iter::Step for BigInt #68

Open iosmanthus opened 6 years ago

iosmanthus commented 6 years ago

Maybe it will be more convenient to write the following code after adding trait Step for BigInt:

for i in BigInt::from(1)..BigInt::from(1000) {
    // do something
}
cuviper commented 6 years ago

std::iter::Step is still unstable, tracked in https://github.com/rust-lang/rust/issues/42168.

That's a big reason why the num-iter crate exists, like its range function. But if your endpoints are small enough to fit in primitive types, like your example, it might actually be faster to just iterate that way and then convert: (1..1000).map(BigInt::from)

Rudxain commented 6 months ago

If anyone reading this needs something like 0.. (RangeFrom trait), here you go:

std::iter::successors(Some(BigUint::zero()), |n| Some(n + 1u8))

I love successors because it's quite simple and flexible. It helped me refactor many loops into functional-style chains of data