#nested #macro #loops #expressions #chain

no-std loop_chain

Macro for writing nested Loop expressions

3 releases

0.1.1 May 7, 2021
0.1.0 May 7, 2021
0.1.0-beta.0 Apr 27, 2021

#27 in #chain

31 downloads per month

MIT license

10KB
147 lines

loop_chain

Macro for writing nested Loop expressions

github workflow status crates docs

Usage | Examples | Docs

Dependencies

[dependencies]
loop_chain = "0.1.1"

Usage

For expression

fn main() {
    loop_chain::loop_chain! {
        for width in 0..10;
        for height in 0..10;
        then {
            println!("width: {}, height: {}", width, height);
        }
    }
}

the generated code will be the following:

fn main() {
    for width in 0..10 {
        for height in 0..10 {
            println!("width: {}, height: {}", width, height);
        }
    }
}

While expression

fn main() {
    let mut foo = 0;
    loop_chain::loop_chain! {
        while foo < 3;
        foo += 1;
        for x in 0..10;
        then {
            println!("foo: {}, x: {}", foo, x);
        }
    }
}

the generated code will be the following:

fn main() {
    let mut foo = 0;
    while foo < 3 {
        for x in 0..10 {
            println!("foo: {}, x: {}", foo, x);
        }
    }
}

While let expression

fn main() {
    let mut foo = (0..10).collect::<Vec<u8>>();
    loop_chain::loop_chain! {
        while let Some(v) = foo.pop();
        for x in 0..10;
        then {
            println!("v: {}, x: {}", v, x);
        }
    }
}

the generated code will be the following:

fn main() {
    let mut foo = (0..10).collect::<Vec<u8>>();
    while let Some(v) = foo.pop() {
        for x in 0..10 {
            println!("v: {}, x: {}", v, x);
        }
    }
}

Loop expression

fn main() {
    let mut foo = 0;
    loop_chain::loop_chain! {
        loop;
        foo += 1;
        if foo > 3 {
            break
        };
        for x in 0..10;
        then {
            println!("foo: {}, x: {}", foo, x);
        }
    }
}

the generated code will be the following:

fn main() {
    let mut foo = 0;
    loop {
        foo += 1;
        if foo > 3 {
            break
        };
        for x in 0..10 {
            println!("foo: {}, x: {}", foo, x);
        }
    }
}

Reference

No runtime deps