# Context on unwrap()

**URL:** https://internals.rust-lang.org/t/context-on-unwrap/9875
**Category:** libs
**Created:** [April 23, 2019, 6:28pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875 "2019-04-23T18:28:19Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![njaard](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/njaard/32/3788_2.png) [@njaard](https://internals.rust-lang.org/u/njaard)
#### Post date: [April 23, 2019, 6:28pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875/1 "2019-04-23T18:28:19Z")

</div>

It seems like an easy usability feature would be context information on `unwrap()`.

Right now if you unwrap(), the panic produces the message that comes from the error type you’re unwrapping (or simply that you tried to unwrap a None). I would like to see the compiler inject source file and line of the actual call to unwrap.

Why? It’s generally impossible to track down where the unwrap occurred unless you have debugging enabled (not always possible). I imagine that a lot of us have reluctantly had to go through their code replacing all their `unwrap()`s to `expect()`s.

Runtime cost would be that an extra binary string would be stored in the executable. No other performance penalty.

---

<div class="post-metadata">

### Author: ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)
#### Post date: [April 23, 2019, 6:29pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875/2 "2019-04-23T18:29:55Z")

</div>

Is this not fulfilled by `RUST_BACKTRACE`?

---

<div class="post-metadata">

### Author: ![njaard](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/njaard/32/3788_2.png) [@njaard](https://internals.rust-lang.org/u/njaard)
#### Post date: [April 23, 2019, 6:31pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875/3 "2019-04-23T18:31:31Z")

</div>

No, `RUST_BACKTRACE` outputs the context according to the debugging information which may not be present. A backtrace is certainly less readable than something like the error of the form: `unwrap() of None, main.rs:55`

Edit: Also RUST\_BACKTRACE has a runtime penalty whereas my suggestion doesn’t.

---

<div class="post-metadata">

### Author: ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)
#### Post date: [April 23, 2019, 6:33pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875/4 "2019-04-23T18:33:11Z")

</div>

[RFC 2091](https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md) specifies a design to fix this. This RFC has been accepted, but [not yet implemented](https://github.com/rust-lang/rust/issues/47809).

---

<div class="post-metadata">

### Author: ![njaard](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/njaard/32/3788_2.png) [@njaard](https://internals.rust-lang.org/u/njaard)
#### Post date: [April 23, 2019, 6:37pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875/5 "2019-04-23T18:37:22Z")

</div>

Yes, that seems to be exactly it.

---

<div class="post-metadata">

### Author: ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)
#### Post date: [April 25, 2019, 5:18pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875/6 "2019-04-25T17:18:24Z")

</div>

> [@njaard](#):
>
> I imagine that a lot of us have reluctantly had to go through their code replacing all their `unwrap()` s to `expect()` s.

Exactly. Leading to `unwrap` never being used by "a lot of us" ever again.

Imho `unwrap` should never have existed as such, to begin with. It should have taken a string argument like `expect` does, so that people wanting to shoot themselves by not adding context information would have had to feed an empty string as an argument. And for people coming from other languages, feeding an empty string error message "smells" more than using a builtin `::core` function of the language.

* * *

And with `expect` the code reads weirdly:

```rust
// No, this does not expect an unitialised parameter; quite the opposite!
let x = param.expect("Uninitialised parameter `param`");

// vs
let x = param.unwrap("Uninitialised parameter `param`");

```

Although I am perfectly aware that it is now too late to change that, we should keep in mind that deprecating `unwrap` remains in the realm of possibilities.

---

<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: [April 25, 2019, 7:01pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875/7 "2019-04-25T19:01:04Z")

</div>

> [@dhm](#):
>
> And with `expect` the code reads weirdly:
> 
> ```rust
> // No, this does not expect an unitialised parameter; quite the opposite!
> let x = param.expect("Uninitialised parameter `param`");
> 
> ```

I would recommend to write something like this instead:

```rust
let x = param.expect("retrieve initialised parameter `param`");

```

---

<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: [July 24, 2019, 7:01pm UTC](https://internals.rust-lang.org/t/context-on-unwrap/9875/8 "2019-07-24T19:01:04Z")

</div>

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