loop_chain

Crates.ioloop_chain
lib.rsloop_chain
version0.1.1
sourcesrc
created_at2021-04-27 08:15:14.553115
updated_at2021-05-07 18:01:52.23533
descriptionMacro for writing nested Loop expressions
homepagehttps://github.com/TaKO8Ki/loop_chain
repositoryhttps://github.com/TaKO8Ki/loop_chain
max_upload_size
id390070
size14,280
Takayuki Maeda (TaKO8Ki)

documentation

https://docs.rs/loop_chain

README

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

Commit count: 20

cargo fmt