External Publication
Visit Post

`alloc`-only `HashMap`?

Rust Internals [Unofficial] March 11, 2026
Source

There have been times when I wanted HashMap/HashSet in some code which only has alloc to work with, but the default BuildHasher implementation being std-only prevents that from working.

What stops us from just moving the type definition to alloc and removing the default type parameter there, and providing the default again via a type alias in std? I think it's just methods/trait implementations which explicitly call out the RandomState argument (which only exists in std). These are:

  • new/with_capacity: I see no reason why we can't allow that for any S: Default instead, and this would allow new to be const once const traits happen (this also works for the with-allocator equivalents).
    • I think this shouldn't break existing code type inference since the existing (soon to be re-export) std name for it still has the default argument.
  • From<[(K, V); N]>: This can again be made to use Default like above, but the possibility of .into() into a place which might be generic and relying on it for type inference could cause type inference failures. Definitely at least worth a crater run before committing anything.

So I think it might be possible? I'm not an expert on how type inference works so there might be wider fallout than I'm thinking of.

Discussion in the ATmosphere

Loading comments...