Skip to content

Instantly share code, notes, and snippets.

@joedavis
Created July 7, 2014 11:27
Show Gist options
  • Save joedavis/1b849f74127c8bd2c042 to your computer and use it in GitHub Desktop.
Save joedavis/1b849f74127c8bd2c042 to your computer and use it in GitHub Desktop.
#![feature(macro_rules)]
use std::collections::HashMap;
macro_rules! map(
{ $T:ident $($args:tt)* } => {
{
let mut m = $T::new();
map_insert! { m $($args)* }
}
}
)
macro_rules! map_insert(
{ $m:expr } => { { $m } };
{ $m:expr, } => { { $m } };
{ $m:expr, $key:expr => $value:expr $($args:tt)* } => {
{
$m.insert($key, $value);
map_insert! { $m $($args)* }
}
};
{ $m:expr, $key:ident: $value:expr $($args:tt)* } => {
{
$m.insert(stringify!($key), $value);
map_insert! { $m $($args)* }
}
};
)
fn main() {
let pi = "\u03c0";
let m = map! { HashMap,
the_answer: 42.0f32,
pi => 3.14159f32,
"e" => 2.71828f32
};
println!("{}", m);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment