# Option could have a function that returns &mut

**URL:** <https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079>\
**Category:** language design\
**Created:** [February 19, 2021, 2:01pm UTC](https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079 "2021-02-19T14:01:02Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![n002](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/n002/32/7795_2.png) [@n002](https://internals.rust-lang.org/u/n002)\
**Post date:** [February 25, 2021, 6:31pm UTC](https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079/21 "2021-02-25T18:31:54Z")

</div>

> [@kornel](#):
>
> There are two mutually-exclusive concepts here:
> 
> 1. If I don't know if a value exists, I use `Option` , and therefore I _can't_ use it without checking
> 2. If I know the object exists, I can use it without checking, and therefore I _don't_ use `Option`

Hi Kornel,

Thank you.

They are **not** mutually-exclusive. See again please from my previous reply all the projects that use `as_mut().unwrap()`

Maybe it's not so common, but use cases exist.  
Example: you have a list of options where you know for sure that the first and last elements can't be `None`.

---

<div class="post-metadata">

**Author:** ![skysch](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@skysch](https://internals.rust-lang.org/u/skysch)\
**Post date:** [February 25, 2021, 8:12pm UTC](https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079/22 "2021-02-25T20:12:48Z")

</div>

I use `.as_mut().unwrap()` all the time, that doesn't mean I want to replace it with something else. Don't try to cite other people's code as an example of suboptimal code just because it exists. That's not a compelling argument.

---

<div class="post-metadata">

**Author:** ![n002](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/n002/32/7795_2.png) [@n002](https://internals.rust-lang.org/u/n002)\
**Post date:** [February 25, 2021, 8:51pm UTC](https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079/23 "2021-02-25T20:51:11Z")

</div>

Having another option doesn't mean that everybody have to or will use the new option.

Also, it's just your opinion that it's suboptimal. 🙂  
It's not like a quoted any code, those people know what they're doing.

You just said that you use all the time. So do you like to write suboptimal code?

---

<div class="post-metadata">

**Author:** ![skysch](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@skysch](https://internals.rust-lang.org/u/skysch)\
**Post date:** [February 25, 2021, 9:08pm UTC](https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079/24 "2021-02-25T21:08:39Z")

</div>

You already have that option. You're not proposing to add an option, you're proposing to add the option to `std` (where everyone _will_ have to learn to understand it.)

---

<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:** [February 25, 2021, 9:23pm UTC](https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079/25 "2021-02-25T21:23:45Z")

</div>

If you really feel like you are using this all the time, you can make an extension trait

```rust
trait OptionExt {
    type Item;
    
    fn unwrap_mut(&mut self) -> &mut Self::Item;
}

impl<T> OptionExt for Option<T> {
    type Item = T;
    
    fn unwrap_mut(&mut self) -> &mut Self::Item { self.as_mut().unwrap() }
}

```

Then you can use it like:

```rust
use some::path::OptionExt;
let inner = some_option.unwrap_mut();

```

However, I don't agree with adding it to `std`. I already see people stumbling over `Option`'s _ **42** _ methods, and dozens of trait implementations. `Option` is stuffed to the brim with "helpful" features (sure many of these are useful, but do we really need all _42_ of them?). We don't need to add more.

---

<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:** [May 26, 2021, 9:24pm UTC](https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079/26 "2021-05-26T21:24:02Z")

</div>

Closed on May 26, 2021, 9:24pm UTC

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

[Previous page](https://internals.rust-lang.org/t/option-could-have-a-function-that-returns-mut/14079.md?page=1)
