Created
April 26, 2020 19:32
-
-
Save sug0/a3e11c2f28289bd78bab26e696d1983d to your computer and use it in GitHub Desktop.
C style switch in Rust
This file contains hidden or 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
| macro_rules! switch { | |
| ($( $cond:expr => $result:expr ),+ $(,)?) => { | |
| match true { | |
| $( _ if $cond => $result ),+, | |
| _ => unreachable!(), | |
| } | |
| } | |
| } | |
| fn main() { | |
| let x = 32; | |
| switch! { | |
| x > 55 => println!("yeeeeeepppp"), | |
| true => println!("fug"), | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment