We ought to have something like this for sure.

I wonder if we can do something a little more generic though, perhaps
using FromIterator.  Not quite as pretty, but

let x: HashMap = from_iter![(key, val), (key2, val2), (key2, val2)]

A dict! macro could do the same thing but allow for the more pleasing
expr => expr,

On Sat, May 31, 2014 at 9:59 AM, Gulshan Singh <[email protected]> wrote:
> Since there's a vec! in the standard library, should we also include some
> macro for HashMaps/Dictionaries? Here's an example of it's implementation
> and usage:
>
> #![feature(macro_rules)]
>
> extern crate collections;
>
> macro_rules! dict(
>     ($($key:expr => $val:expr),*) => ({
>         let mut h = collections::HashMap::new();
>         $(
>             h.insert($key, $val);
>         )*
>         h
>     })
> )
>
> fn main() {
>     let d = dict!["key1" => "value1", "key2" => "value2"];
>     for (k, v) in d.iter() {
>         println!("{}: {}", k, v);
>     }
> }
>
>
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to