Skip to content

Instantly share code, notes, and snippets.

@Meyermagic
Last active August 29, 2015 14:02
Show Gist options
  • Save Meyermagic/30b376c16f10e0376f34 to your computer and use it in GitHub Desktop.
Save Meyermagic/30b376c16f10e0376f34 to your computer and use it in GitHub Desktop.
#![feature(macro_rules)]
macro_rules! alternative(
($in_expr:expr, $pat:pat, $out_true_expr:expr, $out_false_expr:expr) => (
match $in_expr {
$pat => $out_true_expr,
_ => $out_false_expr,
}
);
)
macro_rules! is_match(
($expr:expr, $pat:pat) => (
alternative!($expr, $pat, true, false)
);
)
macro_rules! extract(
($in_expr:expr, $pat:pat, $out_expr:expr) => (
alternative!($in_expr, $pat, Some($out_expr), None)
);
)
#[deriving(Show)]
enum Foo {
FooA,
FooB(u64)
}
fn main() {
let x = FooB(17);
let c = is_match!(x, FooB(15..20));
let d = extract!(x, FooB(a @ 15..20), a + 3);
println!("{} {} {}", x, c, d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment