# Deprecate unwrap in favor of more meaningful method namess

**URL:** <https://internals.rust-lang.org/t/deprecate-unwrap-in-favor-of-more-meaningful-method-namess/18542>\
**Category:** language design\
**Created:** [March 20, 2023, 6:33am UTC](https://internals.rust-lang.org/t/deprecate-unwrap-in-favor-of-more-meaningful-method-namess/18542 "2023-03-20T06:33:08Z")\
**Posts on this page:** 6\
**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:** [March 20, 2023, 6:33am UTC](https://internals.rust-lang.org/t/deprecate-unwrap-in-favor-of-more-meaningful-method-namess/18542/1 "2023-03-20T06:33:08Z")

</div>

For both Option and Result, unwrap() really is a misnomer IMO. First off, they shouldn't both be called the same thing.

"expect()" on the other hand is very meaningful, and very well named. But let's get back to unwrap(). For the Option scenario, it does kind of make some sense, but also kind of not. You might have something (a Some) you can unwrap or you might have none which there's nothing to unwrap cause it's none. I'm just talking the "non technical" thought process related to this. We simply can't unwrap a None - it's a None (and so yes, I get that this panics ..., but it also just doesn't "read well"). For option, I'd go with "get\_some\_val()" (or something like that) Something like "get\_ok\_val()" would be best for Result.

The "?" operator is great. When clicking on variant names in documentation for enums, it might be helpful to include "this is not a type, just an enum variant" because, while this is "Rust 101", it's easy to not "see" because you're looking at types all day, and it just looks like another type.

You'd be surprised how much THIS is the stuff that trips people up including myself. Takes a long time not to "talk stupid" coming up on rust ...

All this made me think of a possible language feature which is "function/method aliasing". The idea is to simply be able to refer to any method by another name (be it internal or a third party library). Instead of another function (renamed) that calls into the one that has the name you don't prefer, it's a compile time feature that simply replaces it pre-compilation with the actual method name (ie, zero cost).

My guess is that people that come from languages that have ADT pick up Rust in no time. If never used them before, long time ...

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [March 20, 2023, 7:43am UTC](https://internals.rust-lang.org/t/deprecate-unwrap-in-favor-of-more-meaningful-method-namess/18542/2 "2023-03-20T07:43:26Z")

</div>

It would be way too much churn to deprecated `unwrap` now. [There's a clippy lint if you want to ban it in your own code bases.](https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used)

An ecosystem where every enclave invents their own conventions to rename fundamental methods to something they prefer sounds much worse to me than adhering to a universal name, even if that name is flawed.

> [@JeffG](#):
>
> The "Some" type needs more documentation. Assuming it's a compiler built-in based on current documentation.

[It's a tuple variant of the `Option` type.](https://doc.rust-lang.org/std/option/enum.Option.html) The constructor is in the prelude, which is why you can use it directly. [You can do the same for your own `enum`s.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ac22cb850e90374f9ed9443e3d7ca307)

You can't define `?` for your own types on stable yet, [but here's the tracking issue.](https://github.com/rust-lang/rust/issues/84277)

---

<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:** [March 20, 2023, 7:53am UTC](https://internals.rust-lang.org/t/deprecate-unwrap-in-favor-of-more-meaningful-method-namess/18542/3 "2023-03-20T07:53:02Z")

</div>

Thanks, I don't know what I was thinking regarding Some.

Algebraic Data Types are also a place where people like me "don't see it" easily. I do see now that I was reading variant type as a an actual type, but in code it seems that way.

Perhaps this is an area of the "upfront" training should be focused since it's such an "Enum Centric Language", but you don't necessarily see it right away so most coming from languages that don't have algebraic data types don't "process it". It's the biggest mental hurdle coming from typical OO languages that don't have enums that hold data. It's more of a mental hurdle that I originally thought.

---

<div class="post-metadata">

**Author:** ![robinm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/robinm/32/6255_2.png) [@robinm](https://internals.rust-lang.org/u/robinm)\
**Post date:** [March 20, 2023, 2:51pm UTC](https://internals.rust-lang.org/t/deprecate-unwrap-in-favor-of-more-meaningful-method-namess/18542/4 "2023-03-20T14:51:33Z")

</div>

> [@JeffG](#):
>
> Perhaps this is an area of the "upfront" training should be focused since it's such an "Enum Centric Language", but you don't necessarily see it right away so most coming from languages that don't have algebraic data types don't "process it". It's the biggest mental hurdle coming from typical OO languages that don't have enums that hold data. It's more of a mental hurdle that I originally thought.

If you have any idea on how to explain it better, please open issues or PRs to the relevant training materials (maybe the Rust book). It’s unfortunately one of those area that are hard to understand, but once you understand them you don’t even remember what was hard about 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:** [March 20, 2023, 3:55pm UTC](https://internals.rust-lang.org/t/deprecate-unwrap-in-favor-of-more-meaningful-method-namess/18542/5 "2023-03-20T15:55:33Z")

</div>

It's too late to change it. It's everywhere in examples, docs, and books. Rust is meant to be stable now, and not cause unnecessary churn.

Even if you just add a new method, that will cause a ton of questions what's the difference between them and which one should be used.

Unwrap already has replacements `?`, `map`, `if let`, etc. so there's no need to add a better function that you shouldn't be using anyway.

---

<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:** [June 18, 2023, 3:56pm UTC](https://internals.rust-lang.org/t/deprecate-unwrap-in-favor-of-more-meaningful-method-namess/18542/6 "2023-06-18T15:56:18Z")

</div>

Closed on June 18, 2023, 3:56pm UTC

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