# Adding Option.expect\_none

**URL:** <https://internals.rust-lang.org/t/adding-option-expect-none/10481>\
**Category:** libs\
**Created:** [June 28, 2019, 2:52pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481 "2019-06-28T14:52:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![SOF3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sof3/32/5346_2.png) [@SOF3](https://internals.rust-lang.org/u/SOF3)\
**Post date:** [June 28, 2019, 2:52pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/1 "2019-06-28T14:52:05Z")

</div>

There exists `Option.expect`, `Result.expect`, `Result.expect_err`, but not `Option.expect_none`.

I understand that the three methods above are used for unwrapping the underlying value, but for functions that return `()`, unwrapping is a convenient way to panic upon unexpected returns.

Right now, to panic if a function doesn’t return `None`, I have to wrap it with `assert!(expr_that_returns_option.is_none(), "Error message")`.

It would be much more readable, convenient and motivating to add sanity checks if an `expect_none()` is introduced.

---

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [June 28, 2019, 3:00pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/2 "2019-06-28T15:00:11Z")

</div>

Personally I’d prefer just using a Result\<(), ()\>.

---

<div class="post-metadata">

**Author:** ![SOF3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sof3/32/5346_2.png) [@SOF3](https://internals.rust-lang.org/u/SOF3)\
**Post date:** [June 28, 2019, 3:12pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/3 "2019-06-28T15:12:09Z")

</div>

My particular use case was where the Option came from a library. I could have used `ok_or()`, but that’s super ugly.

---

<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:** [June 28, 2019, 4:26pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/4 "2019-06-28T16:26:38Z")

</div>

So the main advantage of `expect_none()` (vs. assert) would be that you could easily add it to the end of a method chain. That’s a similar reason to why postfix `.await` was chosen though, so you may find the libs team receptive to this.

---

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [June 28, 2019, 7:10pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/5 "2019-06-28T19:10:05Z")

</div>

True, but .await results in a useful value (so further chaining is possible). But expect\_none() on an Option just results in (), so it lacks much of the chaining benefit that .await has.

---

<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:** [June 28, 2019, 7:24pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/6 "2019-06-28T19:24:00Z")

</div>

I think it’s fine that this wouldn’t enable further chaining – it still lets you avoid wrapping the expression.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [June 28, 2019, 8:23pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/7 "2019-06-28T20:23:50Z")

</div>

I would like this, I find it to be more clear than the assert and the parallel to `Result` makes it desirable. I have reached for something like `expect_none` more than a few times, and it would be nice to see it added.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [June 29, 2019, 2:09pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/8 "2019-06-29T14:09:00Z")

</div>

I’d love this! I often want to assert, after inserting into a `HashMap`, that this key was not used before. There currently is no nice way to do this. With this change, I could do

```rust
map.insert(key, value).expect_none();

```

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [July 11, 2019, 2:11pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/9 "2019-07-11T14:11:31Z")

</div>

Convention would require unwrap\_none(self) -\> () and expect\_none(self, msg: &str) -\> ().

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [July 11, 2019, 2:20pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/10 "2019-07-11T14:20:19Z")

</div>

Of if we had macros in tail position, i.e. where `macro!(foo)` is same as `foo.macro!()`, then you could do:

```rust
map.insert(key, value).is_none().assert!();

```

---

<div class="post-metadata">

**Author:** ![vorner](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/vorner/32/3310_2.png) [@vorner](https://internals.rust-lang.org/u/vorner)\
**Post date:** [July 11, 2019, 2:41pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/11 "2019-07-11T14:41:11Z")

</div>

I don’t know, it seems to be pretty small motivation to bloat the library with this particular method. I mean, you can come up with infinite number of methods you’d find handy from time to time, even though they can be easily combined from smaller parts. We can’t just put infinite number of methods in 😇.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [July 11, 2019, 4:19pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/12 "2019-07-11T16:19:22Z")

</div>

But we have a very similar method on `Result`, so there is some precedent to adding `expect_none(...)`. This isn’t coming from nowhere. Also this would be a tiny addition that would be very helful when writing tests or expressing invariants, so I see it as a net win.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [July 11, 2019, 6:26pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/13 "2019-07-11T18:26:43Z")

</div>

This would also be nice to have for warnings – `unwrap_none` is exactly what [https://github.com/rust-lang/rust/pull/62431#discussion\_r302180901](https://github.com/rust-lang/rust/pull/62431#discussion_r302180901) wants to be able to mention in a `must_use` warning for `is_none`, for consistency with the warnings on `is_some`, `is_ok`, & `is_err`.

---

<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:** [July 11, 2019, 7:08pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/14 "2019-07-11T19:08:29Z")

</div>

> <https://github.com/rust-lang/rust/pull/62596>

---

<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:** [October 9, 2019, 7:11pm UTC](https://internals.rust-lang.org/t/adding-option-expect-none/10481/15 "2019-10-09T19:11:29Z")

</div>

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