3 releases

0.1.3 Aug 15, 2021
0.1.2 Aug 14, 2021
0.1.1 Aug 14, 2021
0.1.0 Aug 14, 2021

#1962 in Procedural macros

MIT/Apache

10KB
196 lines

for_ch

for_ch named "for_each", "for_chain"(or even "4ch"), the crate provides a macro to flatten the nested for-loop and if-let.

Example

for_ch! {
    for x in 0..10;                         // forall x in 0..10,
    // you can add a label before `for`
    for y in x..10, for _ in 0..5;          // forall y in x..x+5, 
    // zipping
    if let Some(z) = foo(x, y).await?;      // exists z. Some(z) = foo(x, y).await?
    // if let guard
    if x - y < z;                           // satisfies x - y < z
    // guard
    println!("x = {}, y = {}, z = {}", x, y, z);
}

would expend to

for x in 0..10 {
    for y in (x..10).zip(0..5) {
        if let Some(z) = foo(x, y).await? {
            if x - y < z {
                println!("x = {}, y = {}, z = {}", x, y, z);
            }
        }
    }
}

Dependencies

~1.5MB
~34K SLoC