Instantly share code, notes, and snippets.

@waynenilsen /dict.rs
Last active Nov 12, 2015

Embed
What would you like to do?
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