Last active
August 6, 2016 18:47
-
-
Save colin-kiegel/c64128d1aa32ba21decffc8cbad28962 to your computer and use it in GitHub Desktop.
rust macro equivalent for `if ($x == $y) { branch!() }`
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
macro_rules! match_and_then { | |
{ $val:tt .. {$( $default_action:tt )*}; $( {$( $pattern:tt )+} => {$( $action:tt )*} ;)*} => { | |
macro_rules! __callback_match_and_then { | |
$( {$( $pattern )+} => {$( $action )*}; )* | |
{ $any:tt } => {$( $default_action )*}; | |
} | |
__callback_match_and_then!($val); | |
} | |
} | |
#[test] | |
fn try_eat() { | |
match_and_then!{ | |
apple .. { println!("what's that?") }; | |
{ apple } => { println!("yummy!") }; | |
{ nothing } => { println!("I'm starving..") }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires rustc 1.12 (current nightly) - due to line 5 (i.e. the default case).