Skip to content

Instantly share code, notes, and snippets.

@samoylovfp
Last active April 22, 2017 22:27
Show Gist options
  • Save samoylovfp/9f57c5b8c74692894c13aec6c279b210 to your computer and use it in GitHub Desktop.
Save samoylovfp/9f57c5b8c74692894c13aec6c279b210 to your computer and use it in GitHub Desktop.
macro_rules! map {
() => (::std::collections::HashMap::new());
($($key: expr => $value: expr),*) => ({
map!{map!{}, $($key => $value),*}
});
($map_instance: expr, $($key: expr => $value: expr),*) => ({
let mut m = $map_instance;
$( m.insert($key, $value); )*
m
});
}
fn main() {
let mut m = map!{};
m.insert("asdf", "fgh");
println!("Strs: {:?}", m);
let m = map!{::std::collections::BTreeMap::new(), 5+6 => 11};
println!("Numbers: {:?}", m);
let mut m = map!{'a' => 'c'};
m.insert('d', 'c');
println!("Chars: {:?}", m);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment