Skip to content

Instantly share code, notes, and snippets.

@PurpleMyst
Created April 15, 2018 14:05
Show Gist options
  • Save PurpleMyst/f371c143dce70c20ec587f65053551dd to your computer and use it in GitHub Desktop.
Save PurpleMyst/f371c143dce70c20ec587f65053551dd to your computer and use it in GitHub Desktop.
A macro for an HashMap literal written in Rust.
use std::collections::HashMap;
macro_rules! hashmap {
{$($key:expr => $value:expr),*} => {{
let mut __hm = HashMap::new();
$(
__hm.insert($key, $value);
)*
__hm
}}
}
fn main() {
let hm = hashmap!{1 => 2, 3 => 4};
println!("{:?}", hm);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment