Macro lender_derive::for_
source · for_!() { /* proc-macro */ }
Expand description
Syntax sugar for iterating over a Lender
.
This function-like procedural macro expands a syntax of the form
for_!(PATTERN in EXPR BLOCK);
where PATTERN
is a valid pattern for a for
loop, EXPR
is an expression that
implements IntoLender
and BLOCK
is a block of code, into a while let
loop that
iterates over a Lender
obtained from the IntoLender
:
let mut ___ඞඞඞlenderඞඞඞ___ = (EXPR).into_lender();
while let Some(PATTERN) = ___ඞඞඞlenderඞඞඞ___.next() BLOCK
For example, the following code
for_!(x in 0..10 {
println!("{}", x);
});
iterates over the integers [0. .10), printing them,
using a Lender
obtained by
automagically adapting an Iterator
(in this case, a Range
).
Note that the outer parentheses are part of the standard Rust syntax for function-like procedural macros, and thus can be replaced, for example, with brackets.
For an example of a more complex usage, see the following code, which iterates over
the elements of an enum
, but only on the first two variants:
#[derive(Debug)]
enum Three {
A,
B,
C,
}
#[test]
pub fn test_bar() {
for_!(x @ (Three::A | Three::B) in [Three::A, Three::B, Three::C].into_into_lender() {
dbg!(x);
});
}
In this case, since an array is an IntoIterator
,
but not an Iterator
, we need to
adapt it manually.
Note that these examples have the sole purpose of showing the syntax of the macro: in these cases a standard iterator would be simpler and more efficient.