Created
February 18, 2017 08:44
-
-
Save selfup/ecd96bcffc7847ffb93d4140b91f529d to your computer and use it in GitHub Desktop.
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! 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