Skip to content

Instantly share code, notes, and snippets.

@colin-kiegel
Last active August 6, 2016 18:47
Show Gist options
  • Save colin-kiegel/c64128d1aa32ba21decffc8cbad28962 to your computer and use it in GitHub Desktop.
Save colin-kiegel/c64128d1aa32ba21decffc8cbad28962 to your computer and use it in GitHub Desktop.
rust macro equivalent for `if ($x == $y) { branch!() }`
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..") };
}
}
@colin-kiegel
Copy link
Author

requires rustc 1.12 (current nightly) - due to line 5 (i.e. the default case).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment