-
Notifications
You must be signed in to change notification settings - Fork 20
ACP: hash_map! macro to create HashMap
s such as vec!
#578
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
Comments
See also rust-lang/rfcs#542 |
Wouldn't this be part of the libs? should I keep this issue open or close it in favor of of that RFC? |
This is the right place, issues on the RFC repo only have limited significance. |
I’ve seen a The type needs to be specified which is slightly less nice, but the upside is a single macro works for everything. let hm: HashMap<_, _> = collection! { “foo”: 10, “bar”: 123 };
let bm: BTreeMap<_, _> = collection! { “foo”: 10, “bar”: 123 };
let hs: HashSet<_> = collection! { “foo, “bar”, “baz” };
// also custom types if they implement FromIter
let mm: MyMap<_> = collection! { … };
// Some redundancy with vec!
let v: Vec<_> = collection![1, 2, 3]; |
The pros of this is that if you are passing that invocation as argument or you are using a reference to it right after the type is inferred, and no problem after that. The cons of this may be complexity on maintaining that macro, I don't know if rust works with the single principle practice but I feel it may be to complex. Another con I find is the redundancy of it, when you read code that you have never read and you see a function invocation, sometimes you may imagine what's up if you can read the arguments, but |
See also rust-lang/rfcs#542 (comment) |
Alternative, we just confirm |
It's not the |
I can open a draft pull request if necessary to integrate the macro. I'd like to contribute to rust in some way. |
Proposal
Add a
hash_map!
macro that would possibly generate aHashMap
instead of usingHashMap::from([])
which doesn't feel that idiomatic.This isn't a performance addition, and wouldn't really change anything else more than add an idiomatic way of creating
HashMap
s.Problem statement
Adding this macro would allow to make code cleaner, instead of having an iterable of tuples that is then collected to a HashMap we would have a more idiomatic macro at the style of
vec!
and this would possibly be an addition and motivation to make this for further structures.Motivating examples or use cases
I made a library, which has templates, to fill these there is a
HashMap<K: ToString, V: ToString>
parameter, creating that is quite bulky for the user. The only way for it to not be bulky is to create that step by step, but sometimes this parameter will be simply aHashMap::from
instantiation.Solution sketch
The macro would look something along the lines of
and this could directly generate a call to
HashMap::from
such asAlternatives
This is too small to be made a crate, and re-implementing the macro on every project feels bulky.
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: