Skip to content

Instantly share code, notes, and snippets.

@mdrokz
Created November 8, 2021 15:15
Show Gist options
  • Save mdrokz/3b487f924fd41ef43e97bf80a6f20df8 to your computer and use it in GitHub Desktop.
Save mdrokz/3b487f924fd41ef43e97bf80a6f20df8 to your computer and use it in GitHub Desktop.
Rust macro for creating hashmaps using js object syntax
macro_rules! map {
($($v: literal => $s: expr),*) => {{
let mut h = HashMap::new();
$(
h.insert($v,$s);
)*
h
}}
}
fn main() {
// Created HashMap using simple object syntax
let v = map! {
"loop" => 300,
"meow" => 200,
"keow" => 100
};
let s = v.get("loop");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment