Created
July 8, 2019 01:06
-
-
Save eth-p/63991b2fb7a89c9df59b9f7af493208e to your computer and use it in GitHub Desktop.
A small ternary conditional operation macro.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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