rust-lang / rust-lang/hashbrown

Cannot use `or_insert_with` for `HashMap::<usize, HashMap<usize, i64>>` - is this supposed to be supported?

Open
#595 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3k
Forks
358
Avg merge
11h 57m
Merged PRs (30d)
2

Description

I have a program like this:

use hashbrown::HashMap;

fn main() {
    let mut value_by_col_by_row = HashMap::<usize, HashMap<usize, i64>>::new();
    value_by_col_by_row.entry_ref(&0).or_insert_with(HashMap::new).insert(0, 1);
}

Unfortunately, cargo build produces the following error:

   Compiling rust v0.1.0 (/my/project/path)
error[E0277]: the trait bound `usize: From<&usize>` is not satisfied
    --> src/main.rs:5:39
     |
5    |     value_by_col_by_row.entry_ref(&0).or_insert_with(HashMap::new).insert(0, 1);
     |                                       ^^^^^^^^^^^^^^ the trait `From<&usize>` is not implemented for `usize`
     |
note: required by a bound in `EntryRef::<'a, 'b, K, Q, V, S, A>::or_insert_with`
    --> /my/home/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.15.2/src/map.rs:4197:19
     |
4195 |     pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V
     |            -------------- required by a bound in this associated function
4196 |     where
4197 |         K: Hash + From<&'b Q>,
     |                   ^^^^^^^^^^^ required by this bound in `EntryRef::<'a, 'b, K, Q, V, S, A>::or_insert_with`
help: consider borrowing here
     |
5    |     (&value_by_col_by_row.entry_ref(&0)).or_insert_with(HashMap::new).insert(0, 1);
     |     ++                                 +

For more information about this error, try `rustc --explain E0277`.
error: could not compile `rust` (bin "rust") due to 1 previous error

I'm not sure if I'm doing something wrong or if this is not supposed to be a supported use case?

$ cargo --version
cargo 1.83.0 (5ffbef321 2024-10-29)
$ rustc --version
rustc 1.83.0 (90b35a623 2024-11-26)

#####

[dependencies]
hashbrown = "0.15.2"

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Rust example and inspect hashbrown/src/map.rs around EntryRef::or_insert_with, especially the K: Hash + From<&Q> bound shown in the compiler error. Compare the entry_ref API behavior with the expected nested HashMap use case and its existing tests; done means the supported behavior and any required API or documentation change are clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.