I'm still new to Rust (and even newer to writing macros), but I don't see how `from_iter!` would be able to figure out what type to return.
I think a `dict!` macro would be worth it for the `=>` syntax, even if `from_iter!` gets implemented. I'm not sure if it could somehow use `from_iter!` though. On Sat, May 31, 2014 at 6:35 PM, Matthew McPherrin <[email protected]> wrote: > 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
