Skip to content

Instantly share code, notes, and snippets.

@waynenilsen
Last active November 12, 2015 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynenilsen/0c7a9e42fbc8581592c2 to your computer and use it in GitHub Desktop.
Save waynenilsen/0c7a9e42fbc8581592c2 to your computer and use it in GitHub Desktop.
python-like dict syntax in rust thanks to macros
use std::collections::HashMap;
macro_rules! dict {
( $( $k:expr => $v:expr ),* ) => {{
let mut m = HashMap::new();
$(
m.insert($k, $v);
)*
m
}};
}
fn main() {
let x = dict!(
"hello" => "world"
,"hello2" => "world2"
);
for (k, v) in x.iter() {
println!("{}: \"{}\"", *k, *v);
}
/** output
hello: "world"
hello2: "world2"
**/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment