Created
July 7, 2014 11:27
-
-
Save joedavis/1b849f74127c8bd2c042 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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