macro_rules! ifor {
($($label:lifetime:)? for $pat:pat in $($rest:tt)*) => { ... };
($pat:pat in $($rest:tt)*) => { ... };
}
Expand description
An extension of for in
loops with better support for infinite iterators.
This macro presents a superset of regular for
loops:
it works both with finite and infinite iterators.
ยงExamples
Use with a finite iterator:
use infinite_iterator::ifor;
ifor!(item in [1, 2, 3] {
println!("{item}");
});
Use with an infinite iterator:
use infinite_iterator::ifor;
ifor!(item in 0.. {
println!("{item}");
std::thread::sleep(std::time::Duration::from_secs(5));
})
Infinite iterators additionally support breaking with a value:
use infinite_iterator::ifor;
let item = ifor!(item in 0.. {
if item > 10 {
break item;
}
});
assert_eq!(item, 11);
You can use loop labels with an ifor!
too,
as long as you write out the keyword for
:
use infinite_iterator::ifor;
ifor!('outer: for a in 0..10 {
ifor!('inner: for b in 0..10 {
println!("{a}, {b}");
if a + b > 16 {
break 'outer;
}
});
});