Macro iterate

Source
macro_rules! iterate {
    ($( $ts:tt )*) => { ... };
}
Expand description

Create an ad hoc iterator.

§Usage

The macro is used just like defining a closure. The return type of it’s body has to be Option<T> for some type T. So at the minimum: iterate!{ None }

The expression iterate! {...} is of type impl Iterator<T>.

Any captured variables are moved (like with move || {...} closures).

You can use return statements in the body of iterate!.

§Example

use ad_hoc_iterator::iterate;

fn count_to(n: usize) -> impl Iterator<Item = usize> {
    let mut i = 0;
    iterate! {
        if i < n {
            i += 1;
            Some(i-1)
        } else {
            None
        }
    }
}