Skip to content

Instantly share code, notes, and snippets.

@jxqu3
Created November 23, 2023 07:17
Show Gist options
  • Select an option

  • Save jxqu3/eb2631ffb832626701cf3f369e4e8ac2 to your computer and use it in GitHub Desktop.

Select an option

Save jxqu3/eb2631ffb832626701cf3f369e4e8ac2 to your computer and use it in GitHub Desktop.
Rust macro to use match as nested ifs, like empty switches in Go
#[macro_export]
// This is a macro to use a match with boolean conditions, like a empty switch in Go.
// Made by github.com/Esper89 in the Rust discord!
macro_rules! cond {
($($cond:expr => $value:expr),* $(, _ => $dft:expr)? $(,)?) => {
match () {
$(() if $cond => $value,)*
() => ($($dft)?),
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment