Skip to content
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

Closed
domoritz opened this issue Dec 20, 2015 · 9 comments
Closed

Quick initializer for maps #1420

domoritz opened this issue Dec 20, 2015 · 9 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@domoritz
Copy link

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.

fn main() {
    let vec = [1, 2, 3, 4];
    print!("{:?}", vec);

    let map = {"foo": 0, "bar": 1};
//              ^^^^^^^^^^^
//              does not work
    print!("{:?}", map);
}

See example at http://is.gd/P0yeNQ

@withoutboats
Copy link
Contributor

First, [1, 2, 3, 4] is not a Vec<i32> actually, its an [i32; 4]. There is no literal syntax for vectors, but there is a very ergonomic macro vec!, so you can create a vector with vec![1, 2, 3, 4]. I expect what you're asking for is a similar macro.

There is a third-party crate called maplit which provides macros like this.

@ticki
Copy link
Contributor

ticki commented Dec 20, 2015

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.

@domoritz
Copy link
Author

domoritz commented Dec 20, 2015

You are right. I meant to use the !vec macro. Although in my tests it doesn't really matter.

The maplit macros are exactly what I was looking for. However, as a user I am a bit surprised that the !vec macro is in the standard library but hashmap! is not. Feel free to close this issue if you disagree.

@ticki
Copy link
Contributor

ticki commented Dec 20, 2015

Feel free to close this issue if you disagree.

That's not how the RFC process works. Opinions and disagreements are allowed.

However, as a user I am a bit surprised that the !vec macro is in the standard library but hashmap! is not.

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 #[macro_use]).

See also Nick Cameron's macro syntax proposal: http://www.ncameron.org/blog/macro-plans-syntax/

@withoutboats
Copy link
Contributor

However, as a user I am a bit surprised that the !vec macro is in the standard library but hashmap! is not.

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.

@steveklabnik
Copy link
Member

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.

@apasel422
Copy link
Contributor

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();

@Stebalien
Copy link
Contributor

You may also be interested in rust-lang/rust#14726 and https://github.com/kmcallister/literator

@bluss
Copy link
Member

bluss commented Dec 23, 2015

This is a duplicate of #542

@Centril Centril added T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC. labels Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

8 participants