Vec has a macro to assist in initialising a Vec with given contents, however none of the other collections in libstd have one. Many other languages have syntax for declaring e.g. a hash map in one go, including Python, Ruby, C++, and C#. Could we do something similar for at least the more commonly used collections, such as HashMap (and maybe others)?
I was thinking along these lines:
let my_vec = vec![1, 2, 3]; // this is for Vec
let my_hashmap = hashmap!{“one”: 1, “two”: 2, “three”: 3}; // this is for HashMap
I’m not sure if this syntax is possible, but I assume it is as vec! has square brackets instead of parentheses. What are your thoughts? Any other collections this could be useful for?
I somewhat dislike the x => y syntax. It has no precedence within Rust, and it reminds me of Perl (or Ruby, if I’m feeling generous). We should take care not to re-use any operator that could be used inside keys or values. Thus, I can see the following alternatives:
x=y (Lua) has the benefit of looking like an assignment within the namespace of the map, at the cost of looking like an assignment. Since we would not want to mix assignments and our macro, it would be ok to re-use the = operator.
x:y (JSON, python, just about everywhere) has the benefit of not looking like an assignment and being familiar to the huge group of python/Javascipt/…etc. developers at the cost of looking like a Rust type declaration.
x->y looks like a bastardization of a function and a binding and like => takes two sigils, but (IMHO) looks a bit cleaner and cannot be confused with either an assignment or a type declaration.
I actually quite like “=>” for the syntax. It is already used in matching, and this has similar semantics - match “one” with 1.
I dislike the others, especially the second as you wouldn’t be able to include type decelerations in the list, and the third as you couldn’t use lambdas with explicit return types. The first one looks too much like assignment for me.
Agreed, the => syntax is used for pattern matching. Ok, so there is some precedence in Rust, I’m gonna retract my statement; I guess I can live with it.
Hmmm. I suppose it would be entirely backwards compatible, except that vec!
would probably be removed. That would mean it needs to be done before or
after 1.x.
vec! is strictly better than seq! though; seq requires type hints for the type of collection, while vec doesn’t. (vec might still need type hints for the contents, but this is quite rare in my experience, particularly with integer fallback)
I a actually have map!() macro somewhere in depth of my code. But I like universal approach of seq!(): I have terrible memory, and the less things to remember, three better.