# The problem(s) with unwrap

**URL:** <https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046>\
**Category:** Uncategorized\
**Created:** [December 20, 2024, 1:47pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046 "2024-12-20T13:47:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffG](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffg/32/9783_2.png) [@JeffG](https://internals.rust-lang.org/u/JeffG)\
**Post date:** [December 20, 2024, 1:47pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/1 "2024-12-20T13:47:30Z")

</div>

You guessed it. It's another unwrap rant. The biggest problems are:

1. It's just code clutter and hurts readability - could have post parenthesis marker like u, such as get\_data()u; and that u represents that the compiler needs to add .unwrap() there, but it should be different for option and result so maybe s for some and r for result - something like that.
2. You don't know if the call to unwrap is for an option or result - should be separated! Very poor readability.

- I think even the bots will appreciate it
-

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [December 20, 2024, 1:59pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/2 "2024-12-20T13:59:32Z")

</div>

Most of my `.unwrap()` usage is in testing code…everywhere else does either `?` or `.map_err(ErrType::variant_ctor_func)`. Or `.expect("if this fires, the source is wrong")` (e.g., in-source regex parses).

As for readability…I think method chaining is cleaner than a (bespoke) syntax sugar thing. Would it only work for `Option` and `Result`? How would user types opt-in to having a `returns_user_type()u` thing happen? Is it just making a method called "unwrap" special? Though it seems you'd prefer to have it be type-specific even…so I have no idea how user types would get this. And one of the really nice things about Rust (to me) is that `Option` and `Result` are just plain library code (no magic). `Try` is still compiler magic for non-nightly uses…but that is still "just a trait" once it is available.

As for bots…how does one measure their appreciation?

---

<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:** [December 20, 2024, 2:43pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/3 "2024-12-20T14:43:04Z")

</div>

Everything you don't like about `.unwrap()` is very well known, and has been fixed by the `?` (try) operator.

Use of `.unwrap()` is not recommended except for basic examples, test code, and where failures indicate a bug in the program.

---

<div class="post-metadata">

**Author:** ![burntsushi](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/burntsushi/32/279_2.png) [@burntsushi](https://internals.rust-lang.org/u/burntsushi)\
**Post date:** [December 20, 2024, 2:58pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/4 "2024-12-20T14:58:40Z")

</div>

> [@kornel](#):
>
> Use of `.unwrap()` is not recommended except for basic examples and test code.

I recommend its use. I use it in `regex`, `regex-syntax`, `regex-automata`, `csv`, `bstr`, `ripgrep`, `jiff` and pretty much every crate I've ever published.

See [Using unwrap() in Rust is Okay - Andrew Gallant's Blog](https://burntsushi.net/unwrap/) for more elaboration on my position. (I feel like I link this too much, but then again, I wrote it precisely because I kept repeating myself. So now I can just link to that.)

---

<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:** [December 20, 2024, 5:12pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/5 "2024-12-20T17:12:17Z")

</div>

I fully agree with your position on `unwrap()`. I usually just don't go into so much detail, because I mainly see `.unwrap()` overused (my theory is that learning materials and docs use `unwrap()` a lot for brevity, and this makes use of `.unwrap()` seem more normal and common than it should be).

If I want to simplify the advice, I think it's better to simplify it towards using too little `.unwrap()` than too much.

---

<div class="post-metadata">

**Author:** ![JeffG](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffg/32/9783_2.png) [@JeffG](https://internals.rust-lang.org/u/JeffG)\
**Post date:** [December 20, 2024, 5:46pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/6 "2024-12-20T17:46:48Z")

</div>

I passed that whole thing a long time ago regarding the differences, but there's lots of tutorials with unwrap and people see it and say "that's not readable code" (then go to another language). You don't see it later on. For those looking into rust, it's one of those things just like "MyNewStr".to\_string() - lolol

---

<div class="post-metadata">

**Author:** ![burntsushi](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/burntsushi/32/279_2.png) [@burntsushi](https://internals.rust-lang.org/u/burntsushi)\
**Post date:** [December 20, 2024, 5:47pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/7 "2024-12-20T17:47:24Z")

</div>

I hear you. It's tough to simplify here sadly.

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [December 20, 2024, 7:16pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/8 "2024-12-20T19:16:59Z")

</div>

I also pretty much agree with @burntsushi 's blog post. To summarize what it says: it is appropriate to use `unwrap` or `expect` when

- you have an operation that returns a Result or Option
- if this operation fails (returns Err or None), that indicates a bug in the program
- it is impractical to restructure the program so that the failure can be proven impossible at compile time

And I think that those rules provide an indirect rebuttal to @JeffG's request for shorter notation for `unwrap`. It is _not_ "just code clutter and hurts readability". It is a warning sign: here is a place where the program _might_ have a bug, and we couldn't find a good way to make the compiler prove it doesn't, so humans should pay closer attention to the code in this area. It _should_ stand out, for the same reason we make `unsafe` blocks and annotations stand out.

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [December 20, 2024, 9:35pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/9 "2024-12-20T21:35:47Z")

</div>

For that same reason I prefer expect over unwrap (outside of tests). It means that there is at least _some documentation_ on why I thought this could not happen in practice.

There is a clippy lint that can be set up for this, to warn on unwrap but not expect. I'm on my phone so I won't provide an example, but it shouldn't be hard to find.

---

<div class="post-metadata">

**Author:** ![ryanavella](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ryanavella/32/13254_2.png) [@ryanavella](https://internals.rust-lang.org/u/ryanavella)\
**Post date:** [December 20, 2024, 10:07pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/10 "2024-12-20T22:07:50Z")

</div>

> [@zackw](#):
>
> It is a warning sign: here is a place where the program _might_ have a bug, and we couldn't find a good way to make the compiler prove it doesn't, so humans should pay closer attention to the code in this area. It _should_ stand out, for the same reason we make `unsafe` blocks and annotations stand out.

In the same spirit as `// SAFETY` comments, I often write a justifying comment for many of my `unwrap`s and `expect`s to explain why the error case is unreachable. Then when it inevitably panics on an edge case, I can go review the comment to see where my past self's reasoning went wrong.

---

<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:** [December 20, 2024, 10:09pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/11 "2024-12-20T22:09:14Z")

</div>

> [@JeffG](#):
>
> You don't know if the call to unwrap is for an option or result - should be separated! Very poor readability.

Can you elaborate _why_ you want to know this in this case?

Would you also also want `.iter_on_vec()` and `.iter_on_slice()` and `.iter_on_hashmap()` and such, so those are separated?

---

<div class="post-metadata">

**Author:** ![JeffG](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffg/32/9783_2.png) [@JeffG](https://internals.rust-lang.org/u/JeffG)\
**Post date:** [December 21, 2024, 12:21pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/12 "2024-12-21T12:21:33Z")

</div>

You just see it more clearly. Like when something is returned from a function call, and then you call unwrap or ? on the result - what am I calling unwrap or ? on. Option and Result are two VERY DIFFERENT domains with the same methods - only adds confusion.

I think what I was getting at with this post wasn't so much picking on unwrap versus ?

It's that there is a LOT that could be implicit by nature, and this could be implicit by nature, and toggled via setting in cargo.toml project settings with additional configuration like which operator to use when implicit. So there would be like an "implicit\_mode" setting - by default it's off. When you turn it on, the following will become implicit assuming the variation declaration is explicit, or rust otherwise has the context needed to make it happen:

1. ? is implicit. (or unwrap if chosen)

2. into() is implicit

3. .to\_string() is implicit (when var is mut)

4. etc

Additionally, for times when you only want implicit mode in certain modules, or blocks, could have a module level derive macro that will simply surround the module with implicit{} block, and also an inline "implicit{}" block for fine level control of sections of code without resorting to module or project level (cuz the \*\*\* didn't want it).

---

<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:** [December 21, 2024, 1:03pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/13 "2024-12-21T13:03:39Z")

</div>

> [@JeffG](#):
>
> Option and Result are two VERY DIFFERENT domains with the same methods - only adds confusion.

But why is that a problem?

To me `.unwrap()` on both does the same thing — gets the inner value or panics. I don't see them as very different.

`?` is constrained on the return type of the function. You can't use it on `Option` when the function returns `Result`, you have to convert it first with `.ok_or(err)` or similar, and vice-versa with `.ok()`. To use `?` you will need to know whether the function returns `Option` or `Result`, but I don't see how that could be improved by having separate methods like `.unwrap_option()` and `.unwrap_result()` — you would still need to know the type returned by the function to know which one to call.

> So there would be like an "implicit\_mode" setting - by default it's off

You're describing a solution, but I still don't see what problem that solution is supposed to fix. There are many different reasons for things to be explicit:

> **[Not Explicit](https://boats.gitlab.io/blog/post/2017-12-27-things-explicit-is-not/)**
>
> Oftentimes when I am conversing about the design of Rust with other users - as on RFCs or the internals forum - I witness a peculiar remark about “explicitness.” It usually goes something like this:
> I do not like Feature Design X because it is too...

---

<div class="post-metadata">

**Author:** ![JeffG](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffg/32/9783_2.png) [@JeffG](https://internals.rust-lang.org/u/JeffG)\
**Post date:** [December 21, 2024, 1:40pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/14 "2024-12-21T13:40:06Z")

</div>

Extra unnecessary code clutter - ruffage

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tczajka/32/8923_2.png) [@tczajka](https://internals.rust-lang.org/u/tczajka)\
**Post date:** [December 21, 2024, 1:43pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/15 "2024-12-21T13:43:56Z")

</div>

> [@JeffG](#):
>
> Option and Result are two VERY DIFFERENT domains with the same methods - only adds confusion.

Despite the usual dogma that they are semantically different, I don't really see them as semantically different at all, more like `Option` is a special case: `Option<T> = Result<T, ()>`. It's a result with no error attached when missing. I wish it was defined like that because I often need to convert between the two types and it's hard to remember the two different APIs. Consider `u32::checked_add`: it could have returned `Result<u32, Overflow>`, but it returns `Option<u32>` because `Overflow` would have been a ZST anyway.

---

<div class="post-metadata">

**Author:** ![pacak](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pacak/32/11934_2.png) [@pacak](https://internals.rust-lang.org/u/pacak)\
**Post date:** [December 22, 2024, 2:41pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/16 "2024-12-22T14:41:11Z")

</div>

> [@JeffG](#):
>
> Option and Result are two VERY DIFFERENT domains with the same methods - only adds confusion.

As long as we are not talking about using them for storing things they both represent the idea of failing computation with the only difference that `Option` doesn't care why the computation failed while `Result` does. In this context `?` is a monadic bind operator and the behavior is exactly the same for both. Both `unwrap` and `expect` means "I know this computation succeeds, get the result out, if not - there's a problem with the code", with the difference that `expects` allows you to explain why things succeed.

---

<div class="post-metadata">

**Author:** ![JeffG](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffg/32/9783_2.png) [@JeffG](https://internals.rust-lang.org/u/JeffG)\
**Post date:** [December 22, 2024, 7:31pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/17 "2024-12-22T19:31:01Z")

</div>

Semantically different. I mean, they are different domains. They both happen to be enum driven. One is (essentially) a container for a value, which may or may not be there.

One is a (essentially) a container for a function result Very different things. Just because they are enum driven doesn't mean the language should tie them together in some weird way.

The language gets all tangled with these 2 things (doesn't happen in other languages)

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [December 22, 2024, 10:46pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/18 "2024-12-22T22:46:46Z")

</div>

> [@JeffG](#):
>
> Semantically different.

I disagree with that statement.

The semantics of both `.unwrap()` and `.expect()` are "consume the container and return the value contained within, and if that's not possible then panic", with `.expect()` having the additional semantics of printing a message when it panics. Importantly, that holds equally for both `Option<T>` and `Result<T, E>`.

> [@JeffG](#):
>
> One is a (essentially) a container for a function result Very different things. Just because they are enum driven doesn't mean the language should tie them together in some weird way.

You seem to be using them as such, which is fair enough.

But to reduce those types to merely that is selling them short I my view: I have used `Result<T, E>` as a field type before (that is, for storage purposes), and if I deem it necessary, I'll do it again.

Similarly, I have also used `Option<T>` both as a function result, and as the type of a fieldstruct field.

To me the only difference is "do I want to differentiate between different forms of 'value not present'?". If so, I use a Result. If not, an Option.

> [@JeffG](#):
>
> The language gets all tangled with these 2 things (doesn't happen in other languages)

Could you give some examples of what you mean by this? I ask because to me they're fairly neatly separated.

---

<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:** [December 22, 2024, 11:18pm UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/19 "2024-12-22T23:18:23Z")

</div>

Regarding

> [@JeffG](#):
>
> could have post parenthesis marker like u, such as get\_data()u; and that u represents that the compiler needs to add .unwrap() there

> [@JeffG](#):
>
> So there would be like an "implicit\_mode" setting - by default it's off. When you turn it on, the following will become implicit assuming the variation declaration is explicit, or rust otherwise has the context needed to make it happen:
> 
> 1. ? is implicit. (or unwrap if chosen)

Please note that the question of adding any shorthand operator/syntax, (or implicit rules) for doing `.unwrap()` is [explicitly something that’s **unlikely** to happen](https://github.com/rust-lang/lang-team/blob/master/src/frequently-requested-changes.md#an-operator-for-unwrap) according to the Language team, and there’s a recommendation that proposals of such nature [there’s a whole list] would need to look into prior discussions and address the existing concerns

> If something appears on this page, that doesn't mean Rust would _never_ consider making any similar change. It does mean that any hypothetical successful proposal to do so would need to address _at a minimum_ all of these known concerns, whether by proposing a _new and previously unseen_ approach that avoids all the concerns, or by making an _extraordinary_ case for why their proposal outweighs those concerns.
> 
> Hopeful proposers of any of these ideas should document their extensive research into the many past discussions on these topics and ensure they have something new to offer.

* * *

If we aren’t only talking about _new syntax_, the idea of

> 1. You don't know if the call to unwrap is for an option or result - should be separated!

could still seem relevant in the context of changes to the method names in the standard library…

…except that can’t work either: `unwrap` is an existing, stable, and very widely used API, it can’t be changed.

* * *

So the question of how related or not `Option` and `Result` are is not really that relevant at all, _in case_ it discussed only for the purpose evaluating such proposals.

---

<div class="post-metadata">

**Author:** ![JeffG](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffg/32/9783_2.png) [@JeffG](https://internals.rust-lang.org/u/JeffG)\
**Post date:** [December 23, 2024, 12:25am UTC](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046/20 "2024-12-23T00:25:21Z")

</div>

It just doesn't LOOK like other languages. It gets all tangled up here. It does.

You gotta keep in mind that many people in tech are switching learning many different things.

Regarding the shorthand, not sure what you were referring to exactly, but I'm saying that it should be as implicit as possible. The language is WAY TOO VERBOSE. Compare with python - night and day. having an implicit mode where things like ? weren't even needed, would be better imho

[Next page](https://internals.rust-lang.org/t/the-problem-s-with-unwrap/22046.md?page=2)
