{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreiatgcjv4hmncqzkoahazl23aiwwhxyai6u663wql74d7gwbgzr6jq",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mgt5l7juxjo2"
},
"path": "/t/alloc-only-hashmap/24071#post_3",
"publishedAt": "2026-03-11T23:53:56.000Z",
"site": "https://internals.rust-lang.org",
"tags": [
"That would break inference.",
"play.rust-lang.org",
"Rust Playground"
],
"textContent": "JarredAllen:\n\n> `new`/`with_capacity`: I see no reason why we can't allow that for any `S: Default` instead,\n\nThat would break inference.\n\n> But hold on, how is the compiler inferring `S` in this snippet?\n>\n>\n> let mut my_map = HashMap::new();\n> my_map.insert(\"hello\", \"there\");\n> println!(\"{:?}\", my_map);\n>\n>\n> What possibly determines it? Well, we can find out the hard way by using `HashMap::default` instead:\n>\n>\n> let mut my_map = HashMap::default();\n> my_map.insert(\"hello\", \"there\");\n> println!(\"{:?}\", my_map);\n>\n>\n>\n> error[E0282]: type annotations needed for `HashMap<&str, &str, S>`\n> --> src/main.rs:4:22\n> |\n> 4 | let mut my_map = HashMap::default();\n> | ---------- ^^^^^^^ cannot infer type for type parameter `S` declared on the struct >`HashMap`\n> | |\n> | consider giving `my_map` the explicit type `HashMap<_, _, S>`, where the type parameter `S` is specified\n>\n>\n> YUP! `HashMap::new` hardcoding `S = RandomState` is _load bearing_. Any code that creates a short-lived `HashMap` as part of some algorithm would run into this horrible error about how `S` is unknown!\n\nAn alias is too transparent to help in the expression case on its own.\n\nplay.rust-lang.org\n\n### Rust Playground\n\nA browser interface to the Rust compiler to experiment with the language",
"title": "`alloc`-only `HashMap`?"
}