Crate iter_from_closure [] [src]

Provides the function iter_from_closure intended to quickly transform a mutable closure of type FnMut() -> Option<Item> into an Iterator where Item = Item.

This is useful for creating one-time iterators from some state that you have. This is meant to serve as a pain free alternative to the boilerplate of creating a new struct and implementing [Iterator] for every such occasion.


use iter_from_closure::iter_from_closure;

let mut count = 5;
let iter = iter_from_closure(|| {
    let c = count;
    count = c - 1;
    if c > 0 { Some(c) } else { None }

assert_eq!(vec![5, 4, 3, 2, 1], iter.collect::<Vec<_>>());



An Iterator implemented by its underlying FnMut() -> Option<Item>.



Converts a closure of form FnMut() -> Option<Item> into a struct implementing Iterator<Item = Item>.