Skip to content

Instantly share code, notes, and snippets.

@eth-p
Created July 8, 2019 01:06
Show Gist options
  • Save eth-p/63991b2fb7a89c9df59b9f7af493208e to your computer and use it in GitHub Desktop.
Save eth-p/63991b2fb7a89c9df59b9f7af493208e to your computer and use it in GitHub Desktop.
A small ternary conditional operation macro.
#![allow(unused)]
macro_rules! tern {
(true ? $yes:tt : $no:tt) => {
$yes
};
(false ? $yes:tt : $no:tt) => {
$no
};
($cond:tt ? $yes:tt : $no:tt) => {
if $cond { $yes } else { $no }
};
}
fn main() {
let val = tern!(true ? "Hi" : "Bye");
println!("Value: {}", val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment