# \[Pre-RFC\] HashMap/BTreeMap Entry method or\_insert\_with\_key

**URL:** <https://internals.rust-lang.org/t/pre-rfc-hashmap-btreemap-entry-method-or-insert-with-key/12093>\
**Category:** libs\
**Created:** [April 6, 2020, 5:04pm UTC](https://internals.rust-lang.org/t/pre-rfc-hashmap-btreemap-entry-method-or-insert-with-key/12093 "2020-04-06T17:04:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChaiTRex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chaitrex/32/6699_2.png) [@ChaiTRex](https://internals.rust-lang.org/u/ChaiTRex)\
**Post date:** [April 6, 2020, 5:04pm UTC](https://internals.rust-lang.org/t/pre-rfc-hashmap-btreemap-entry-method-or-insert-with-key/12093/1 "2020-04-06T17:04:16Z")

</div>

Both `HashMap` and `BTreeMap` have an idiom like this:

```rust
let value = map.entry(key).or_insert(value_if_empty)

let value = map.entry(key).or_insert_with(|| expensive_value_if_empty)

```

The problem is that `or_insert_with` doesn't provide the lambda with the map key you're currently dealing with. This is sometimes desirable. For example, [the Rust book has a `Cacher` type](https://doc.rust-lang.org/book/ch13-01-closures.html#limitations-of-the-cacher-implementation) that takes an expensive-to-compute lambda and later allows you to provide an argument to the lambda and receive either the cached or newly-computed result value.

It would be nice to be able to make a `Cacher` method like this:

```rust
    fn value(&mut self, arg: K) -> &V {
        let calculation = self.calculation;
        self.cache.entry(arg).or_insert_with_key(|key| calculation(key))
    }

```

As it is, you either have to clone the key or replicate the following uglier implementation of the `or_insert_with_key` method:

```rust
    pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
        match self {
            Occupied(entry) => entry.into_mut(),
            Vacant(entry) => {
                let value = default(entry.key());
                entry.insert(value)
            },
        }
    }

```

I propose inserting that code into both the `HashMap` and `BTreeMap` `Entry` modules. Is this a good idea? Are there any better names or implementations or other suggestions?

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [April 6, 2020, 5:48pm UTC](https://internals.rust-lang.org/t/pre-rfc-hashmap-btreemap-entry-method-or-insert-with-key/12093/2 "2020-04-06T17:48:47Z")

</div>

It seems like a reasonable idea to me, however in your example:

> [@ChaiTRex](#):
>
> ```rust
> fn value(&mut self, arg: K) -> &V {
> self.cache.entry(arg).or_insert_with_key(|&arg| self.calculation(arg))
> }
> 
> ```

I don't expect this will work, because `self.cache` is mutably borrowed for the entry -- that will make `self` methods inaccessible for your calculation. You could only use distinct fields of `self`, and you'd have to pre-borrow them outside of your closure so it doesn't try to capture `self`. The explicit `match` would also have a problem with `self` methods, but would have no problem with distinct `self` fields.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [April 6, 2020, 7:11pm UTC](https://internals.rust-lang.org/t/pre-rfc-hashmap-btreemap-entry-method-or-insert-with-key/12093/3 "2020-04-06T19:11:30Z")

</div>

Yeah, this is a standard problem I run into regularly: I want to use things like the entry API, but I can't when the table lives in `self` and filling the table requires touching other bits of `self`. I tend to end up either doing two lookups (`self.sometable.get` then `self.sometable.insert`), or open-coding things I would otherwise have put in a helper function because Rust can't see inside the helper function to see that it doesn't touch `self.sometable`.

I wish that Rust's borrow checker didn't mind "peering inside" other methods of `Self` and doing cross-function analysis in that specific case to see which members the called method uses. I can understand not peering inside arbitrary other functions, but this issue primarily comes up with struct methods and struct fields, and those seem like the most justifiable cases for the borrow checker to peer inside.

---

<div class="post-metadata">

**Author:** ![ChaiTRex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chaitrex/32/6699_2.png) [@ChaiTRex](https://internals.rust-lang.org/u/ChaiTRex)\
**Post date:** [April 6, 2020, 7:36pm UTC](https://internals.rust-lang.org/t/pre-rfc-hashmap-btreemap-entry-method-or-insert-with-key/12093/4 "2020-04-06T19:36:52Z")

</div>

> [@cuviper](#):
>
> I don't expect this will work, because `self.cache` is mutably borrowed for the entry

Thanks for the heads up. With a modified `hashbrown` crate, I got the code to work and edited my original post.

---

<div class="post-metadata">

**Author:** ![ChaiTRex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chaitrex/32/6699_2.png) [@ChaiTRex](https://internals.rust-lang.org/u/ChaiTRex)\
**Post date:** [April 10, 2020, 5:15pm UTC](https://internals.rust-lang.org/t/pre-rfc-hashmap-btreemap-entry-method-or-insert-with-key/12093/5 "2020-04-10T17:15:58Z")

</div>

I was advised to submit this as a pull request rather than as an RFC, and I've done so at [https://github.com/rust-lang/rust/pull/70996](https://github.com/rust-lang/rust/pull/70996)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 9, 2020, 5:15pm UTC](https://internals.rust-lang.org/t/pre-rfc-hashmap-btreemap-entry-method-or-insert-with-key/12093/6 "2020-07-09T17:15:59Z")

</div>

Closed on July 9, 2020, 5:15pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
