num-bigint icon indicating copy to clipboard operation
num-bigint copied to clipboard

implement Trait std::iter::Step for BigInt

Open iosmanthus opened this issue 7 years ago • 2 comments

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
}

iosmanthus avatar Nov 01 '18 05:11 iosmanthus

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)

cuviper avatar Nov 01 '18 22:11 cuviper

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

Rudxain avatar May 07 '24 12:05 Rudxain