Skip to content

Instantly share code, notes, and snippets.

@yasukotelin
Created August 6, 2020 14:31
Show Gist options
  • Save yasukotelin/5d1f9ab5bd50b96b35854f1e0201b3a8 to your computer and use it in GitHub Desktop.
Save yasukotelin/5d1f9ab5bd50b96b35854f1e0201b3a8 to your computer and use it in GitHub Desktop.
macro_rules! hashmap {
($( $key: expr => $val: expr ),*) => {{
let mut map = ::std::collections::HashMap::new();
$( map.insert($key, $val); )*
map
}}
}
@yasukotelin
Copy link
Author

@yasukotelin
Copy link
Author

Usage

use std::collections::HashMap;

let hash: HashMap<&str, i32> = hashmap!["hello" => 1, "world" => 2];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment