Open
Description
Code
use fxhash::FxBuildHasher;
use std::collections::HashMap;
pub static X: HashMap<(), ()> = HashMap::with_hasher(FxBuildHasher::new());
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=685acb633567db20ff95170b3221a1bb
Current output
error[E0308]: mismatched types
--> src/lib.rs:4:54
|
4 | pub static X: HashMap<(), ()> = HashMap::with_hasher(FxBuildHasher::new());
| -------------------- ^^^^^^^^^^^^^^^^^^^^ expected `RandomState`, found `BuildHasherDefault<FxHasher>`
| |
| arguments to this function are incorrect
|
= note: expected struct `RandomState`
found struct `BuildHasherDefault<FxHasher>`
note: associated function defined here
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:312:18
|
312 | pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
| ^^^^^^^^^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output
Something that highlights the default value of HashMap
s third generic parameter. I have barely ever used custom hashers so it wasn't top of mind that with_hasher
resulted in a different return type.
Rationale and extra context
I've been writing Rust for many years, but it still took me a minute before it realized what was wrong; a clearer error message would have immediately made the problem obvious :)
Rust Version
rustc 1.89.0-nightly (45acf54ee 2025-06-16)
binary: rustc
commit-hash: 45acf54eea118ed27927282b5e0bfdcd80b7987c
commit-date: 2025-06-16
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
Anything else?
@rustbot label +D-terse