macro_rules! iter { () => { ... }; ($x:tt; $n:expr) => { ... }; (...$x:tt; $n:expr) => { ... }; (...[...$xs:expr; $n:expr] $($tail:tt)*) => { ... }; (...$some:tt $(,)? ) => { ... }; (...$some:tt, ...[...$x:tt; $n:expr] $($tail:tt)*) => { ... }; (...$some:tt, ...[$x:tt; $n:expr] $($tail:tt)*) => { ... }; (...$some:tt, ...$xs:expr $(,)?) => { ... }; (...$some:tt, ...$xs:expr, $($tail:tt)*) => { ... }; (...[$($some:tt)*], $x:tt $(, $($tail:tt)*)?) => { ... }; (...$xs:expr $(, $($tail:tt)*)?) => { ... }; ($x:expr $(, $($tail:tt)*)?) => { ... }; }
Expand description
Chain one more elements or iterables together into one sequence, using “spread” syntax.
let arr = [1, 2, 3];
let vec = vec![8, 9, 10];
assert!(
iter![...arr, 4, ...(5..7), 7, ...vec]
.eq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
iter![a, b, c]
simply returns an iterator over those elements, exactly like
[a, b, c].into_iter()
. But elements can be prefixed with ...
to indicate
a “spread”: they are chained into the resulting iterator with
std::iter::chain()
.
See [lit_vek::vek]
in this crate for more explanation of the syntax.
Examples
use {lit_vek::{iter, CycleN}, std::{array, iter}};
let ints = [1, 2, 3];
// Empty invocations of iter! expand to [std::iter::empty].
let _: iter::Empty<i32> = iter![];
// One or more arguments expand to Array::IntoIter.
let _: array::IntoIter<_, 1> = iter![1];
let _: array::IntoIter<_, 3> = iter![1, 2, 3];
// The `[x; n]` syntax expands to [std::iter::repeat(x).take(n)]
let _: iter::Take<iter::Repeat<_>> = iter![0; 5];
// The `[...x; n]` syntax expands to [`cycle_n`] from this crate
let _: CycleN<_> = iter![...ints; 3];
// And chaining expands to [std::iter::Chain]
let _: iter::Chain<_, _> = iter![...ints, ...ints];
See also
The vek![]
macro in this crate is a drop-in replacement for vec![]
,
but enables the same spread syntax. It is equivalent to
iter![].collect::<Vec<_>>()
.
The itertools::chain!
macro is similar, except it takes only iterable
arguments rather than a mix of iterables and single elements.
ⓘ
chain![a, b, iter::once(c)] == iter![...a, ...b, c]