#iterator #types #extension #ext

iterator-ext

An extension to Rust’s Iterator

3 unstable releases

0.2.1 Jan 2, 2022
0.2.0 Jun 14, 2021
0.1.0 May 2, 2021

#575 in Rust patterns

Download history 32/week @ 2022-06-02 14/week @ 2022-06-09 5/week @ 2022-06-16 16/week @ 2022-06-23 9/week @ 2022-06-30 19/week @ 2022-07-07 7/week @ 2022-07-14 14/week @ 2022-07-21 9/week @ 2022-07-28 83/week @ 2022-08-04 32/week @ 2022-08-11 40/week @ 2022-08-18 53/week @ 2022-08-25 14/week @ 2022-09-01 43/week @ 2022-09-08 16/week @ 2022-09-15

155 downloads per month
Used in shkeleton

MIT license

26KB
724 lines

iterator-ext: An extension to Rust's Iterator trait.

Usage

The crate provides the IteratorExt trait extends the capability of those types that implements Iterator. It provides try_filter(), try_flatten() and more fallible adaptors that are analogous to those of Iterator.

The example demonstrates the usage of the adaptors. It accumulates the values from 0 to 9, and keeps only even outcomes. It raises error when the accumulation exceeds 10.

use iterator_ext::IteratorExt;
//!
let results: Vec<_> = (0..10)
    .map(Ok)
    .try_scan(0, |acc, val| {
        *acc += val;
        if *acc <= 10 {
            Ok(Some(*acc))
        } else {
            Err("exceed limit")
        }
    })
    .try_filter(|val| Ok(val % 2 == 0))
    .collect();
//!
assert_eq!(results, vec![Ok(0), Ok(6), Ok(10), Err("exceed limit")]);

License

MIT license. See LICENSE.txt file.


lib.rs:

An extension to Iterator trait.

The IteratorExt trait extends the capability of those types that implements Iterator. It provides try_filter(), try_flatten() and more fallible adaptors that are analogous to those of Iterator.

The example demonstrates the usage of the adaptors. It accumulates the values from 0 to 9, and keeps only even outcomes. It raises error when the accumulation exceeds 10.

use iterator_ext::IteratorExt;

let results: Vec<_> = (0..10)
    .map(Ok)
    .try_scan(0, |acc, val| {
        *acc += val;
        if *acc <= 10 {
            Ok(Some(*acc))
        } else {
            Err("exceed limit")
        }
    })
    .try_filter(|val| Ok(val % 2 == 0))
    .collect();

assert_eq!(results, vec![Ok(0), Ok(6), Ok(10), Err("exceed limit")]);

No runtime deps