# Get \`&mut Map\` back from entry API

**URL:** <https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003>\
**Category:** libs\
**Created:** [February 13, 2026, 9:15am UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003 "2026-02-13T09:15:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alice](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/alice/32/5044_2.png) [@alice](https://internals.rust-lang.org/u/alice)\
**Post date:** [February 13, 2026, 9:15am UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/1 "2026-02-13T09:15:22Z")

</div>

Consider the following API:

```rust
use std::collections::HashMap;
use std::hash::Hash;

pub fn get_priority_mut<'a, K, V>(
    map: &'a mut HashMap<K, V>,
    key1: &K,
    key2: &K,
) -> Option<&'a mut V>
where
    K: Eq + Hash,
{
    // Check for the first key without mutably borrowing the map
    if map.contains_key(key1) {
        map.get_mut(key1)
    } else {
        // If the first isn't there, we are free to mutably borrow for the second
        map.get_mut(key2)
    }
}

```

I would like to avoid the double-lookup of `key1`, but we can't because of the usual borrow checker limitation. The usual fix for that is to use entry api, but even that doesn't work in this case:

```rust
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::hash::Hash;

// ❌ THIS WILL NOT COMPILE
pub fn get_priority_mut_entry<'a, K, V>(
    map: &'a mut HashMap<K, V>,
    key1: K,
    key2: K,
) -> Option<&'a mut V>
where
    K: Eq + Hash,
{
    match map.entry(key1) { 
        Entry::Occupied(occupied) => Some(occupied.into_mut()),
        Entry::Vacant(_vacant) => {
            match map.entry(key2) {
                Entry::Occupied(occupied2) => Some(occupied2.into_mut()),
                Entry::Vacant(_) => None,
            }
        }
    }
}

```

```plaintext
error[E0499]: cannot borrow `*map` as mutable more than once at a time
  --> src/lib.rs:17:19
   |
 6 | pub fn get_priority_mut_entry<'a, K, V>(
   | -- lifetime `'a` defined here
...
14 | match map.entry(key1) { 
   | --- first mutable borrow occurs here
15 | Entry::Occupied(occupied) => Some(occupied.into_mut()),
   | ------------------------- returning this value requires that `*map` is borrowed for `'a`
16 | Entry::Vacant(_vacant) => {
17 | match map.entry(key2) {
   | ^^^ second mutable borrow occurs here

```

I'd like to propose a potential solution: Add a method to `[Vacant|Occupied]Entry` that consumes the entry and returns `&mut HashMap`. If you had this API, you could write this:

```rust
match map.entry(key1) { 
    Entry::Occupied(occupied) => Some(occupied.into_mut()),
    Entry::Vacant(vacant) => {
        match vacant.into_map().entry(key2) {
            Entry::Occupied(occupied2) => Some(occupied2.into_mut()),
            Entry::Vacant(_) => None,
        }
    }
}

```

which does not run into the borrow checker limitation.

---

<div class="post-metadata">

**Author:** ![HjVT](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hjvt/32/10365_2.png) [@HjVT](https://internals.rust-lang.org/u/HjVT)\
**Post date:** [February 13, 2026, 2:38pm UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/2 "2026-02-13T14:38:08Z")

</div>

Doesn't a simple `if let else` cover this?

```rust
    let a_or_b = if let a @ Some(_) = map.get_mut("a") {
        a
    } else {
        map.get_mut("b")
    };

```

> **[Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=e3e7ac65b2ae8887a0aa73195f12a760)**
>
> A browser interface to the Rust compiler to experiment with the language

A method like `Entry::or_get(alternative_key)` for expressing this succinctly in cases where you are already using the Entry API would be nice to have, that's for sure.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [February 13, 2026, 2:46pm UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/3 "2026-02-13T14:46:39Z")

</div>

Your playground is missing [the essential detail for running into the borrow-checker limitation](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=faf208ed827efda058a64d100a696831): Returning the `Option<&mut String>` value from a function 😉

---

<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:** [February 13, 2026, 4:29pm UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/4 "2026-02-13T16:29:16Z")

</div>

It doesn't _have_ a reference to give back to you -- note that `Entry<'_, K, V>` isn't even parameterized for the original `S` hasher in `HashMap<K, V, S>`.

---

<div class="post-metadata">

**Author:** ![alice](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/alice/32/5044_2.png) [@alice](https://internals.rust-lang.org/u/alice)\
**Post date:** [February 13, 2026, 4:41pm UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/5 "2026-02-13T16:41:07Z")

</div>

Damn!

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [February 13, 2026, 5:14pm UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/6 "2026-02-13T17:14:44Z")

</div>

But, this way of writing it at least [works with polonius](https://rust.godbolt.org/z/oc7E1bE3r).

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [February 13, 2026, 6:47pm UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/7 "2026-02-13T18:47:49Z")

</div>

The suggestion might still be valid for `hashbrown` though, since its `VacantEntry` does have a `S` parameter and contain a `&mut HashMap`.

---

<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:** [February 13, 2026, 7:02pm UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/8 "2026-02-13T19:02:58Z")

</div>

True, and FWIW its `hash_table` entries already have an `into_table` method like that.

---

<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:** [February 16, 2026, 9:37pm UTC](https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003/9 "2026-02-16T21:37:33Z")

</div>

> <https://github.com/rust-lang/hashbrown/pull/686>
>
> This adds methods to get the original \`&'a mut HashMap\` reference from
> \`hash\_ma…p::{Entry, EntryRef, OccupiedEntry, VacantEntry, VacantEntryRef}\`
> to help with current borrow-checker limitations in returning lifetimes
> from different branches.
> 
> \`\`\`rust
> /// Converts the \`Entry\` into a mutable reference to the underlying map.
> pub fn into\_map(self) -\> &'a mut HashMap\<K, V, S, A\>;
> \`\`\`
> 
> There are already similar \`into\_table\` methods on \`hash\_table\` occupied
> and vacant entries, but this also adds that to \`hash\_table::Entry\` for
> consistency.
> 
> This idea was raised in an \[internals thread\]\[1\] for the standard
> library, but it's not possible there since those entries don't have any
> reference to return, especially since they aren't even parameterized by
> the \`S\` hasher at all -- they are wrapping \`RustcEntry\` here.
> 
> \[1\]: https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003
