# Thoughts about the Entry API

**URL:** <https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685>\
**Category:** language design\
**Created:** [August 1, 2017, 12:13pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685 "2017-08-01T12:13:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![marmistrz](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@marmistrz](https://internals.rust-lang.org/u/marmistrz)\
**Post date:** [August 1, 2017, 12:13pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/1 "2017-08-01T12:13:11Z")

</div>

Hi,

I thought that it might be a good idea to add an `expect` method for the entry API. Currently one can do this

```rust
let map: HashMap<i32, i32> = HashMap::new();
let entry = map.entry(7).or_insert_with(|| panic!());

```

This `or_insert_with` is just not great for the readability, IMHO.

The other problem with the Entry API usability is that it consumes the key. Is there any reason why it’s not taken by reference?

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [August 1, 2017, 12:28pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/2 "2017-08-01T12:28:45Z")

</div>

I agree with your thought about `expect` but we are waiting for `try` trait being stabilised.

For the API that consumes the key, it is very intuitive to me actually. In most cases, the key should be `Copy` or at least `Clone` so it is not really matter. But in some cases we need to move the value in.

Also, if it is taken by reference, there have to be some lift time notations in the interface, so it would look ugly.

---

<div class="post-metadata">

**Author:** ![hanna-kruppe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hanna-kruppe/32/6540_2.png) [@hanna-kruppe](https://internals.rust-lang.org/u/hanna-kruppe)\
**Post date:** [August 1, 2017, 12:31pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/3 "2017-08-01T12:31:57Z")

</div>

I’m not sure why someone would use the Entry API just to look up a value and panic if it’s absent. Doesn’t `get_mut` suffice? (It’s wrapping the mutable reference in an Option, but `Option` one does have `expect`.)

---

<div class="post-metadata">

**Author:** ![marmistrz](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@marmistrz](https://internals.rust-lang.org/u/marmistrz)\
**Post date:** [August 1, 2017, 12:41pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/4 "2017-08-01T12:41:02Z")

</div>

If you want to print it and remove it from the `HashMap` if it exists, `get_mut` won’t let you.

---

<div class="post-metadata">

**Author:** ![kennytm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kennytm/32/161_2.png) [@kennytm](https://internals.rust-lang.org/u/kennytm)\
**Post date:** [August 1, 2017, 12:55pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/5 "2017-08-01T12:55:17Z")

</div>

> [@marmistrz](#):
>
> If you want to print it and remove it from the HashMap if it exists, get\_mut won’t let you.

```rust
if let Some(existing_value) = map.remove(&7) {
    println!("{}", existing_value);
}

```

---

<div class="post-metadata">

**Author:** ![hanna-kruppe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hanna-kruppe/32/6540_2.png) [@hanna-kruppe](https://internals.rust-lang.org/u/hanna-kruppe)\
**Post date:** [August 1, 2017, 12:56pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/6 "2017-08-01T12:56:24Z")

</div>

Ah, I see. `or_insert[_with]` won’t give you the `OccupiedEntry`, though (it just returns `&mut V`), which confused me.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [August 1, 2017, 12:56pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/7 "2017-08-01T12:56:39Z")

</div>

> [@marmistrz](#):
>
> The other problem with the Entry API usability is that it consumes the key. Is there any reason why it’s not taken by reference?

Cause the Entry API is meant to insert something into the map when it is not there and return it.

```rust
let map = HashMap::new();
assert_eq!(&*map.entry(1).or_insert("foobar".to_string()), "foobar");
assert_eq!(&*map.entry(1).or_insert("new".to_string()), "foobar");

```

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [August 1, 2017, 4:50pm UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/8 "2017-08-01T16:50:41Z")

</div>

The entry API has many tricky constraints that are non-obvious, which makes the key being a reference or not a big issue. I discussed this a while back here: [Head-Desking on Entry API 4.0](https://internals.rust-lang.org/t/head-desking-on-entry-api-4-0/2156)

The reason insertion methods don’t return an Entry is because this is, in general, non-trivial to do. Inserting can completely restructure the data structure, and reconstituting the entry could be very expensive if this wasn’t desired.

Consider, for instance, a tree without parent pointers. An Entry for such a type is a stack of the searched nodes. Inserting could trigger a total rebalance, requiring a brand-new search stack to be computed.

---

<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:** [March 25, 2019, 8:28am UTC](https://internals.rust-lang.org/t/thoughts-about-the-entry-api/5685/9 "2019-03-25T08:28:51Z")

</div>

Closed on March 25, 2019, 8:28am UTC

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