Skip to content

Do-while macro for rust with labels and pattern matching support.

Notifications You must be signed in to change notification settings

nstrmx/dowhile-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
let mut x = Ok(6);
dowhile!({
    println!("x = {x:?}");
    x = Err("error");
} match x {
    Ok(val) => true,
    Err(err) => {
        println!("err = {err}");
        false
    }
});

// x = Ok(6)
// err = error

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

About

Do-while macro for rust with labels and pattern matching support.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages