# Option::infallible() - a good idea?

**URL:** <https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691>\
**Category:** Uncategorized\
**Created:** [May 12, 2021, 5:39pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691 "2021-05-12T17:39:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![extremegf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/extremegf/32/8130_2.png) [@extremegf](https://internals.rust-lang.org/u/extremegf)\
**Post date:** [May 12, 2021, 5:39pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/1 "2021-05-12T17:39:49Z")

</div>

Do you think it would be a good idea to have infallible() along with unwrap() on Option and Result?

The rationale is that often you know that an operation will never fail. Yet there is no ergonomic way to convey that assumption to the reader of the code. unwrap() is often used as a quick and dirty workaround when you have an option and it is recomended in Rust Book to try to replace unwrap() with proper error handling. expect() on the other hand can convey the meaning but it's verbose.

Compare the options:

```
serde_json::to_string(&vec![1,2]).infallible()

serde_json::to_string(&vec![1,2]).expect("Always serializes correctly")

serde_json::to_string(&vec![1,2]).unwrap()
```

---

<div class="post-metadata">

**Author:** ![ibraheemdev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ibraheemdev/32/13406_2.png) [@ibraheemdev](https://internals.rust-lang.org/u/ibraheemdev)\
**Post date:** [May 12, 2021, 6:00pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/2 "2021-05-12T18:00:23Z")

</div>

If they both do the same thing, I don't see the point, it'll just be confusing. There is also the unsafe `unwrap_unchecked`, which will perform extra optimizations, but generally it's better to `unwrap` unless you are 101% percent sure.

> expect() on the other hand can convey the meaning but it's verbose.

`expect` is meant to give extra context, it's only verbose if your message is verbose. `infallible` would do nothing more than `unwrap`, I don't see how it would be an improvement.

---

<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:** [May 12, 2021, 6:42pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/3 "2021-05-12T18:42:21Z")

</div>

I agree in general, though on the otherhand, all of `todo!()`, `unreachable!()`, `unimplemented!()`, and `panic!()` exist. This is perhaps like going from `panic!()` to `unreachable!()`. Same effect, different…feel?

Usually I just use `.expect()` myself so that if my assumptions are ever wrong, at least something is guaranteed to come out of the woodwork in the error. _Maybe_ if `.infallible()` to a string to explain why it is considered such, it would be more useful, but as merely an alternate `.unwrap()` spelling, meh (IMO).

---

<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:** [May 12, 2021, 6:56pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/4 "2021-05-12T18:56:19Z")

</div>

Why would you want a string over a comment? If the code is unreachable, putting a string in the binary is a waste of memory.

---

<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:** [May 12, 2021, 6:59pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/5 "2021-05-12T18:59:58Z")

</div>

If it compiles down to `unreachable!(string_given)`, then fine. Do `unreachable!()` arguments ever end up anywhere even in debug builds?

---

<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:** [May 12, 2021, 7:15pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/6 "2021-05-12T19:15:25Z")

</div>

`unreachable!` just calls `panic!`, so I'd expect so. Hiding that behind `Option::infallible` would make no difference, because `Option::unwrap` already invokes `panic!` too.

But the alternative here is that you could just write

```rust
serde_json::to_string(&vec![1,2]).unwrap() // infallible

```

And that will compile down to nothing new, because comments are not included in executable code at all. They're also much more flexible and can communicate more subtle and important information than an `.infallible` call ever could. They're always exactly as verbose as you want them to be.

```rust
serde_json::to_string(&vec![1,2]).unwrap() // This is infallible because we returned above on `None`.

```

---

<div class="post-metadata">

**Author:** ![cole-miller](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cole-miller/32/7607_2.png) [@cole-miller](https://internals.rust-lang.org/u/cole-miller)\
**Post date:** [May 12, 2021, 7:55pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/7 "2021-05-12T19:55:40Z")

</div>

Maybe the closest thing in current Rust:

```rust
serde_json::to_string(&vec![1, 2]).unwrap_or_else(|_| unreachable!());

```

---

<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:** [May 12, 2021, 8:13pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/8 "2021-05-12T20:13:39Z")

</div>

Perhaps I'm being unclear, so allow me to rephrase my objection in a different form.

What intent is being clarified by this new function? The intent of calling `unwrap` on an option is that the `Option` is `Some`: calling `unwrap` on a `None` value results in an unfriendly error message from a panic indicating what happened. It is clear from this behavior that there is a bug in the program, and that bug is to be fixed by ensuring `unwrap` is not called on a `None` value.

Whet _more_ is communicated by this new method? When is it _wrong_ to use `unwrap`, where it would be _correct_ to use `infallible`?

---

<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:** [May 12, 2021, 8:18pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/9 "2021-05-12T20:18:15Z")

</div>

I guess I'm more interested in the state that, over time, the invariant holders and the invariant expecters migrate away from each other and then change. The message being in the error message helps to track things down again.

Again, I'm not really thrilled with the new method myself, but just wanted to observe that the difference is similar to that between `panic!()` and `unreachable!()`.

---

<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:** [May 12, 2021, 8:20pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/10 "2021-05-12T20:20:47Z")

</div>

But `panic` will indicate a misuse of an API, whereas `unreachable` is always a bug in the library. To avoid misuse of `Option` in an API, you would rewrite it to not use `Option`, and take the `T` directly. (As I said, the convention is already that `unwrap` indicates a bug. Trying to retcon `unwrap` as a non-bug won't work because all the current code already exists with a contrary meaning, and `expect` is right there for that.)

In other words, if I am to understand what you're saying, the new convention would be that one should call `.infallible` on options which must not be None, but `.unwrap` on ones which may be None even though they should never be? I do struggle to see a difference there. (I.e., If you wanted to panic on bad user input, you should probably just match `None` and give a decent panic message, rather than calling `unwrap` and showing them _that_ garbage.)

---

<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:** [May 12, 2021, 9:01pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/11 "2021-05-12T21:01:00Z")

</div>

"Infallible" to me says something like `Result<_, !>`, where you convey the expectation that it's infallible by using [`into_ok`](https://doc.rust-lang.org/nightly/std/result/enum.Result.html#method.into_ok) to get the value out in a way that won't compile any more if the error type changes to something inhabited.

But that doesn't make sense for `Option`, since there's no type-level way to say it's not `None`. There could be `into_none`, that only works on `Option<impl Into<!>>`, but that feels mostly unhelpful.

I think that `unwrap` does a fine job of just communicating "I'm not expecting this to be `None`".

---

<div class="post-metadata">

**Author:** ![ibraheemdev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ibraheemdev/32/13406_2.png) [@ibraheemdev](https://internals.rust-lang.org/u/ibraheemdev)\
**Post date:** [May 12, 2021, 10:55pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/12 "2021-05-12T22:55:39Z")

</div>

Plus, `unwrap` is already established. If someone sees `infallible`, they'll have to look it up, defeating the point.

---

<div class="post-metadata">

**Author:** ![hyeonu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hyeonu/32/2162_2.png) [@hyeonu](https://internals.rust-lang.org/u/hyeonu)\
**Post date:** [May 13, 2021, 12:51am UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/13 "2021-05-13T00:51:34Z")

</div>

It is recommended in Rust Book to use `.unwrap()` if you know that an operation will never fail.

[https://doc.rust-lang.org/stable/book/ch09-03-to-panic-or-not-to-panic.html#cases-in-which-you-have-more-information-than-the-compiler](https://doc.rust-lang.org/stable/book/ch09-03-to-panic-or-not-to-panic.html#cases-in-which-you-have-more-information-than-the-compiler)

---

<div class="post-metadata">

**Author:** ![extremegf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/extremegf/32/8130_2.png) [@extremegf](https://internals.rust-lang.org/u/extremegf)\
**Post date:** [May 13, 2021, 12:45pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/14 "2021-05-13T12:45:47Z")

</div>

I'm not happy with `infallible` name either. It's neither short nor particluarly clear to a new-commer.

The biggest issue I have with .unwrap() is that during development it makes sense to use unwrap initially, but then it is hard to discern which uses are infallible and which need to be replaced with proper error handling.

```
.expect("infallible")
.unwrap() // TODO: remove

```

Seem like resonable solutions. I only wish we had a standard way of doing this, so that I'm not inventing stuff others have to then learn and/or reinvent.

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [May 13, 2021, 3:22pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/16 "2021-05-13T15:22:46Z")

</div>

I have seen this proposed on the forum previously, as a kind of dependent types application, allowing the following to compile:

```rust
if let None = opt {
    return;
}
let Some(x) = opt; // cannot fail

```

It seems like `Result::into_ok` is about as far as that's going to get without dependent types though.

For my own part, I don't see `.unwrap_or_else(|_| unreachable!())` often enough in my codebases to warrant inclusion in std.

---

<div class="post-metadata">

**Author:** ![yaahc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yaahc/32/6606_2.png) [@yaahc](https://internals.rust-lang.org/u/yaahc)\
**Post date:** [May 13, 2021, 3:24pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/17 "2021-05-13T15:24:39Z")

</div>

I don't think this is a good idea. This really feels like the role of expect. If you think that an unwrap is infallible then you're supposed to put the explanation of why you never expect it to fail in the expect call. I'm pretty sure that's why it's called expect. I would not enjoy just being told that something wasn't supposed to fail because it was infallible, that's barely any more information than you get from an unwrap.

---

<div class="post-metadata">

**Author:** ![kaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kaj/32/1387_2.png) [@kaj](https://internals.rust-lang.org/u/kaj)\
**Post date:** [May 17, 2021, 6:48am UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/18 "2021-05-17T06:48:13Z")

</div>

Also note that if the compiler can know that the `Option` really cannot be `None` or the `Result` cannot be `Err(...)`, it can optimize the check out of `.unwrap()`, so that the generated code really does not execute a conditional (and the binary don't need to contain the error message, if that is true for all unwrap calls in the program).

(This probably requires inlining the function call that generates the Option / Result, and I'm guessing `serde_json::to_string(&vec![1,2])` is probably too complex for this to happen.)

---

<div class="post-metadata">

**Author:** ![extremegf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/extremegf/32/8130_2.png) [@extremegf](https://internals.rust-lang.org/u/extremegf)\
**Post date:** [May 17, 2021, 9:45am UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/19 "2021-05-17T09:45:56Z")

</div>

I guess the answer to the topic question is: Probably not a good idea.

Still It would be nice to have a clear convention on how to mark a logically infallible Option/Result. Currently it's a mix of "unwrap() in inffalible cases is ok" and "expect() should explain why this never fails".

(sorry for the edits)

---

<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:** [May 17, 2021, 4:45pm UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/20 "2021-05-17T16:45:18Z")

</div>

`expect` is an easier to debug version of `unwrap`. Which one to use depends either on how certain you are that a failure will never occur (sometimes it is trivial) or on how you want to behave when your assumptions are wrong (sometimes you want to protect against changing conditions or future code changes, in which case `expect` is much better at acting like a runtime assertion.)

My conventions are that I always document why a failure can't occur, either as a comment on `unwrap`, or an `expect` argument. That choice usually depends on whether that non-failure determination is made by looking at the structure of the code, or the values given to me from outside, respectively.

---

<div class="post-metadata">

**Author:** ![extremegf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/extremegf/32/8130_2.png) [@extremegf](https://internals.rust-lang.org/u/extremegf)\
**Post date:** [May 18, 2021, 8:48am UTC](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691/21 "2021-05-18T08:48:11Z")

</div>

> depends on whether that non-failure determination is made by looking at the structure of the code, or the values given to me from outside, respectively.

That's a pretty good way to make the distinction. I like it.

[Next page](https://internals.rust-lang.org/t/option-infallible-a-good-idea/14691.md?page=2)
