There exist simple utilities such as hash_set![], hash_map!{} and so on, not present in the standard library. I got the following ideas for the Rust language and its standard library:
A set! macro which supports a FromIterator of V entries. It needs to be typed since there's no default ideal type.
A map! macro which supports a FromIterator of (K, V) entries. It needs to be typed since there's no default ideal type.
struct S {
m: HashMap<&'static str, &'static str>,
}
let _ = S {
m: map! { "k" => "v" })
};
fn take_map(map: HashMap<&'static str, &'static str>) {
}
For comparison, Swift’s ArrayLiteralConvertible is essentially the first part of this, though the run-time implementation is suboptimal for a handful of reasons. Swift also has a dedicated DictionaryLiteralConvertible, but map! probably makes more sense for Rust.
That's true, but also note that calling from_iter directly might be more concise and readable in the case where you need to specify the type and don't have a let to hang it on, or want it to look like T::new() and T::from() for consistency. FromIterator is even in the prelude since edition 2021, so it doesn't need to be imported to be used explicitly. So, Foo::from_iter([elem, elem, ...]) will work for every collection that you could also collect() into.