for_ch

Crates.iofor_ch
lib.rsfor_ch
version0.1.3
sourcesrc
created_at2021-08-14 04:50:47.251552
updated_at2021-08-15 05:20:25.329871
descriptionto flatten nested for-loop
homepage
repositoryhttps://github.com/TOETOE55/for_ch
max_upload_size
id435963
size9,896
Danube (TOETOE55)

documentation

https://docs.rs/for_ch/

README

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);
            }
        }
    }
}
Commit count: 8

cargo fmt