Crates.io | cond |
lib.rs | cond |
version | 1.0.5 |
source | src |
created_at | 2023-11-23 15:10:19.793914 |
updated_at | 2023-11-24 21:00:52.171467 |
description | Rust macro to use a match-like syntax as an elegant alternative to nesting if-else statements |
homepage | https://github.com/checkm4ted/cond |
repository | https://github.com/checkm4ted/cond |
max_upload_size | |
id | 1046268 |
size | 7,470 |
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.
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);
}
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 to Esper89 for essentially making the whole macro in the Rust community Discord server.