#do #pattern-matching #dowhile #do-while #controlflow #while

dowhile_rs

Do while macro for rust with labels and nesting support

3 releases

0.1.2 Jan 6, 2025
0.1.1 Jan 5, 2025
0.1.0 Jan 5, 2025

#514 in Rust patterns

Download history 306/week @ 2025-01-01 63/week @ 2025-01-08

369 downloads per month

MIT license

6KB
127 lines

Installation

cargo add dowhile_rs

Example usage

let mut x = 6;
dowhile!({
    println!("x = {x}");
    x += 1;
} x < 3);

// x = 6

Pattern matching

let mut x = Some(6);
dowhile!({
    println!("x = {x:?}");
    x = Some(x.unwrap_or(0) + 1);
} let Some(..3) = x);

// x = Some(6)
let mut x = Some(6);
dowhile!({
    println!("x = {x:?}");
    x = Some(x.unwrap_or(0) + 1);
} let Some(val) = x => val < 3);

// x = Some(6)

Nested

let mut x = 4;
dowhile!({
    println!("x = {x}");

    let mut y = 0;
    dowhile!({
        println!("y = {y}");
        y += 1;
    } y < x);
    
    x += 1;
} x < 4);

// x = 4
// y = 0
// y = 1
// y = 2
// y = 3

Labeled

let mut x = 10;
dowhile!('first: {
    println!("x = {x}");

    let mut y = 0;
    dowhile!('second: {
        if y == 4 {
            break 'first;
        }
        println!("y = {y}");
        y += 1;
    } y < x);
    
    x += 1;
} x < 6);

// x = 10
// y = 0
// y = 1
// y = 2
// y = 3

No runtime deps