Skip to content

Instantly share code, notes, and snippets.

@selfup
Created February 18, 2017 08:44
Show Gist options
  • Save selfup/ecd96bcffc7847ffb93d4140b91f529d to your computer and use it in GitHub Desktop.
Save selfup/ecd96bcffc7847ffb93d4140b91f529d to your computer and use it in GitHub Desktop.
macro_rules! actions({
$( $action: expr => $function: expr ), + } => {{
let mut store = ::std::collections::HashMap::new();
$(store.insert($action as &str, $function as Box<Fn(&str)>);) + store
}};
);
fn main() {
let actions = actions!{
"foo" => Box::new(|arg: &str| println!("foo {}", arg)),
"bar" => Box::new(|arg: &str| println!("bar {}", arg))
};
match actions.get("foo") {
Some(action) => action("stuff"),
None => return,
}
match actions.get("bar") {
Some(action) => action("other stuff"),
None => return,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment