cond

Crates.iocond
lib.rscond
version1.0.5
sourcesrc
created_at2023-11-23 15:10:19.793914
updated_at2023-11-24 21:00:52.171467
descriptionRust macro to use a match-like syntax as an elegant alternative to nesting if-else statements
homepagehttps://github.com/checkm4ted/cond
repositoryhttps://github.com/checkm4ted/cond
max_upload_size
id1046268
size7,470
CheckM4te (checkm4ted)

documentation

https://docs.rs/cond

README

cond

Rust macro to use a match-like syntax as an elegant alternative to many if-else statements.

I got the idea from empty Go switch statements. I thought it could be cool if it was in Rust so I asked if that was possible in the Rust community Discord server. They told me it wasn't unless you used a pretty ugly syntax in a match, and Esper89 (GitHub in credits) made a macro for it. I added some tests and documentation and here's my first Rust crate.

Example

use cond::cond;

fn main() {
    let a = 195;
    cond! {
        a < 5 => println!("a is less than 5"),
        a == 195 => {
            println!("this is the way")
        },
        a > 10 => println!("a is greater than 10"),

        // The conditions are executed by order: if one condition is true, conditions below will not get evaluated
    };

    let b = "";
    let result = cond! { // Or use it as a block to return a value
        b == "something" => false,
        b.chars().count() > 10 => true,
        a < 10000 => true,
        _ => false // You must add a default with the return type if you want to return
    };

    println!("result: {}", result);
}

Usage

You can just add the crate with:

cargo add cond

Or just add the 8 line macro to your project:

macro_rules! cond {
    ($($condition:expr => $value:expr),* $(, _ => $default:expr)? $(,)?) => {
        match () {
            $(() if $condition => $value,)*
            () => ($($default)?),
        }
    };
}

Credits

Credits to Esper89 for essentially making the whole macro in the Rust community Discord server.

Commit count: 22

cargo fmt