Crate loop_chain[][src]

This crate provides a single macro called loop_chain!.

loop_chain! enable you to write long chains of nested Loop expressions such as for, while, while let and loop without the associated rightward drift.

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);
        }
    }
}

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);
        }
    }
}

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);
        }
    }
}

Macros

loop_chain