Open
Description
Now that FromIterator
is in the prelude, I expected to be able to create a HashMap
like using HashMap::from_iter()
:
use std::collections::HashMap;
fn main() {
let h = HashMap::from_iter([("foo", 1), ("bar", 2)]);
}
However, that doesn't compile, saying "type annotations needed for HashMap<&str, i32, S>
":
--> src/main.rs:3:13
|
3 | let h = HashMap::from_iter([("foo", 1), ("bar", 2)]);
| - ^^^^^^^ cannot infer type for type parameter `S` declared on the struct `HashMap`
| |
| consider giving `h` the explicit type `HashMap<&str, i32, S>`, where the type parameter `S` is specified
I would expect <HashMap as FromIterator>::from_iter()
to pick the default hasher in absence of an explicit request for a different one requested, like Vec::from_iter()
compiles and defaults to the global allocator. (HashMap::from()
also compiles, but it seems to be limited to the default hasher.)
Note that this is different from #69123 which is about a confusing diagnostic. This issue is about the error itself, in particular as compared to Vec::from_iter()
.
Tested with Rust 1.56.1 stable.