-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quick initializer for maps #1420
Comments
First, There is a third-party crate called maplit which provides macros like this. |
This is better suited for a macro, since maps are not that common and such sugar isn't worth it. Also, there are multiple types of maps for different use cases, and they're not hard-coded in the language. I'm unsure if such a macro belong in the standard library, though. |
You are right. I meant to use the The maplit macros are exactly what I was looking for. However, as a user I am a bit surprised that the |
That's not how the RFC process works. Opinions and disagreements are allowed.
I don't hold a particular strong opinion on whether libstd should provide a hashmap macro. On one side, it's a neat "sugar" for initializing hashmaps (which is currently a pain). On the other side, I would avoid getting libstd bloated with macros, since importing/exporting macros, currently, is odd (you might have noticed that you don't have to import the macros in the standard library, even though they aren't (and cannot be) a part of the prelude). Macros aren't exported/imported in the "usual" way. They belong to a module and are chosen whether to be exported by the parrent module (using See also Nick Cameron's macro syntax proposal: http://www.ncameron.org/blog/macro-plans-syntax/ |
If I recall correctly, the reasoning was that while vectors are very clearly preferred over other sequential data structures for nearly all use cases, the choice between hashmap and btreemap is less obvious. People were reluctant to preference one with a macro. I don't have a strong opinion. I use maps a lot less in Rust than I do in dynamic languages like Python or Ruby. |
Yes, @withoutboats is right as to why we've historically rejected this. I don't think we currently have an issue open about it, though, so we might as well have this one. |
A non-duck-typed macro supporting both (all) map types will have to wait until we get generic collection traits, but right now one can use let map: HashMap<_, _> = vec![
("foo", 0),
("bar", 1),
].into_iter().collect(); |
You may also be interested in rust-lang/rust#14726 and https://github.com/kmcallister/literator |
This is a duplicate of #542 |
Especially in tests it can be extremely helpful to quickly initialize a map. Right now, you have to create an empty map and call
insert
a couple of times.See example at http://is.gd/P0yeNQ
The text was updated successfully, but these errors were encountered: