Macro mapcomp::iterc[][src]

macro_rules! iterc {
    (@__ $exp:expr; for $item:pat in $iter:expr; if $cond:expr) => { ... };
    (@__ $exp:expr; for $item:pat in $iter:expr) => { ... };
    (@__ $exp:expr; for $item:pat in $iter:expr; if $cond:expr; $($tail:tt)+) => { ... };
    (@__ $exp:expr; for $item:pat in $iter:expr; $($tail:tt)+) => { ... };
    ($exp:expr; $($tail:tt)+) => { ... };
}

Iterator Comprehension

Returns an iterator over the contents of the comprehension. It is analogous to Python's generator comprehensions. Syntactically, it is similar to the vecc![] macro but it returns a lazily evaluated iterator instead of a container. It's use requires the experimental generators feature.

#![feature(generators, generator_trait)]
#[macro_use]
extern crate mapcomp;

let numbers = [8, 3, 5, 7];

let mut powers_of_two = iterc!(1 << x; for x in &numbers);

assert_eq!(Some(256), powers_of_two.next());
assert_eq!(Some(8), powers_of_two.next());
assert_eq!(Some(32), powers_of_two.next());
assert_eq!(Some(128), powers_of_two.next());
assert_eq!(None, powers_of_two.next());

Since it only creates an iterator and not a fully populated container, the comprehension can be created over an unbounded or infinite iterator.

let mut odd_squares = iterc!(x * x; for x in 1..; if x % 2 == 1);

assert_eq!(Some(1), odd_squares.next());
assert_eq!(Some(9), odd_squares.next());
assert_eq!(Some(25), odd_squares.next());
assert_eq!(Some(49), odd_squares.next());