In C++ you can use an initializer to initialize a map, which looks like a map literal and feels nice.
#include <map>
int main() {
auto m = std::map<int, int> { { 1, 2 }, { 3, 4 } };
}
playground
So I suggest we can do something like this:
let m = <HashMap<i32, i32>> { // wrapping type name with <> to indicate type like in UFCS
( 1, 2 ),
( 3, 4 ),
};
which do similar thing as above C++ code.
We already have initializer list built-in so we don't need to tackle about the initializer side.
Let me explain how above Rust code work:
It is desugared to:
let m: HashMap<i32, i32> = vec![(1, 2), (3, 4)].into_iter().collect(); // I hope we can drain array here
playground
The trick is that impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S> is done already.
To sum up, points are:
- start with a type here so we know it's about a type, not for the propose of mentioning some associated item and not to start struct literals and such
- wrap a list in braces and separate with comma
- each item in the list is plain expression, so the syntax is very flexible
Now you might argue that if the desugared form is already okay why should we accept this fancy sugar.
I say this sugaring form is much more human-parsable and can make life easier.
In C++ you can use an initializer to initialize a map, which looks like a map literal and feels nice.
playground
So I suggest we can do something like this:
which do similar thing as above C++ code.
We already have initializer list built-in so we don't need to tackle about the initializer side.
Let me explain how above Rust code work:
It is desugared to:
playground
The trick is that
impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>is done already.To sum up, points are:
Now you might argue that if the desugared form is already okay why should we accept this fancy sugar.
I say this sugaring form is much more human-parsable and can make life easier.