# HashMap insertion

**URL:** <https://users.rust-lang.org/t/hashmap-insertion/140404>\
**Category:** uncategorized\
**Created:** [June 2, 2026, 11:08am UTC](https://users.rust-lang.org/t/hashmap-insertion/140404 "2026-06-02T11:08:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keaudes](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/keaudes/32/28187_2.png) [@Keaudes](https://users.rust-lang.org/u/Keaudes)\
**Post date:** [June 2, 2026, 11:08am UTC](https://users.rust-lang.org/t/hashmap-insertion/140404/1 "2026-06-02T11:08:43Z")

</div>

Is there an actual way to insert an element into a HashMap/HashSet so that:

1. It does not replace the current value if the key was already present
2. It returns if the key was already present in some way or another (bool/Option/Result/etc.)
3. It does not do lookup twice
4. The provided key is not necessarily of type K but is of type Q such that `K : Borrow<Q> + From<Q>` and it only constructs the key (K) from the given lookup key (Q) if it turns out that the key was not yet present and thus now needs to be constructed for insertion
5. It can be done in stable Rust

I don't care if it's a single function or a multitude of statements.

---

<div class="post-metadata">

**Author:** ![derekdreery](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/derekdreery/32/4788_2.png) [@derekdreery](https://users.rust-lang.org/u/derekdreery)\
**Post date:** [June 2, 2026, 11:31am UTC](https://users.rust-lang.org/t/hashmap-insertion/140404/2 "2026-06-02T11:31:09Z")

</div>

I think `entry` API is what you want - but I'm not sure about your key requirements.

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [June 2, 2026, 11:35am UTC](https://users.rust-lang.org/t/hashmap-insertion/140404/3 "2026-06-02T11:35:43Z")

</div>

> **[pub fn entry\_ref\<'a, 'b, Q\>( &'a mut self, key: &'b Q, ) -\> EntryRef\<'a, 'b,......](https://docs.rs/hashbrown/latest/hashbrown/struct.HashMap.html#method.entry_ref)**
>
> Gets the given key’s corresponding entry by reference in the map for in-place manipulation. | A hash map implemented with quadratic probing and SIMD lookup.

in HashBrown does roughly what you want (although it uses `ToOwned`, not `From`).

std does use `hashbrown`, but does not expose its API.  
i do not believe there is anything in std that exposes the API that you want.  
if you do want to use the `std` `HashMap`, i would recommend doing two lookups

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [June 2, 2026, 12:15pm UTC](https://users.rust-lang.org/t/hashmap-insertion/140404/4 "2026-06-02T12:15:35Z")

</div>

the sandard library hashmap has an `.entry()` API, which almost meets your requirements, except for 4.

if you are ok with an external crate, you can use hashbrown, which has an `entry_ref()` api, this API is not exposed in `std`.

```rust
let mut words: HashMap<String, usize> = todo!();
match words.entry_ref("hello") {
    EntryRef::Occupied(occupied_entry) => {
        println!("key already mapped to value {}", occupied_entry.get());
    }
    EntryRef::Vacant(vacant_entry_ref) => {
        println!("key doesn't exist, allocating key and inserting value...");
        vacant_entry_ref.insert(42);
        // alternatively, if you don't want to use `ToOwned` to convert the query key
        //vacant_entry_ref.insert_with_key(String::from("hello"), 42);
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 31, 2026, 12:16pm UTC](https://users.rust-lang.org/t/hashmap-insertion/140404/5 "2026-08-31T12:16:15Z")

</div>

Closed on August 31, 2026, 12:16pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
