Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the definition of the word "unwrap" as used by Rust #82383

Closed
trevyn opened this issue Feb 22, 2021 · 5 comments
Closed

Document the definition of the word "unwrap" as used by Rust #82383

trevyn opened this issue Feb 22, 2021 · 5 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@trevyn
Copy link
Contributor

trevyn commented Feb 22, 2021

I was teaching a newbie some Rust, and doing the usual hand-waving about error handling using unwrap. They asked what 'unwrap' means. I said look it up in the docs. The docs read (paraphrased) "unwrap unwraps". I was embarrassed.

#68918 Don't use the word "unwrap" to describe "unwrap" methods

The English word "unwrap" is used in the name of numerous methods in core and std, but is not defined by the documentation and does not have a commonly accepted concrete definition in computer science. The everyday concept of "unwrapping" is not detailed enough to resolve questions about the precise meaning of the term as it is used in Rust.

Existing methods:

methods
Result unwrap, unwrap_or, unwrap_or_else, unwrap_or_default, unwrap_err
Option unwrap, unwrap_or, unwrap_or_else, unwrap_or_default, unwrap_none (unstable)
Rc try_unwrap
Arc try_unwrap

Based on this existing usage, I propose a definition:

An "unwrap" method in Rust extracts an underlying value from one or more containers, consumes the container(s), and returns an owned value.

Result::unwrap() and Option::unwrap() (and their variants) unwrap two containers in one step: The abstract Result or Option itself, and the concrete Ok or Some tuple variant inside. You might think of unwrap as being shorthand for "unwrap_ok" and "unwrap_some", though these methods do not exist.

Result::unwrap_err() and Option::unwrap_none() (nightly only) express that the Err or None variants are expected instead, and that those containers are the ones that should be unwrapped.

Outstanding questions: Where is the most appropriate place to put a definition of "unwrap"? Is The Book canonical?

Previous discussion in:
#62633 Tracking issue for Option::expect_none(msg) and unwrap_none()
#73077 Stabilize Option::expect_none(msg) and Option::unwrap_none()
#77326 Stabilize Option::unwrap_none and Option::expect_none
#68849 Don't use the word 'unwrap' to describe core 'unwrap' functions
#68918 Don't use the word "unwrap" to describe "unwrap" methods
#23713 Make Option.unwrap documentation more precise
#19149 Rename unwrap functions to into_inner
#430 RFC: Finalizing more naming conventions
etc...

@trevyn
Copy link
Contributor Author

trevyn commented Feb 23, 2021

@steveklabnik Do you have a sense of where in the documentation a canonical definition of the word "unwrap" might belong?

The std documentation appears to strictly break things down by module or other primitive, and this does not intuitively belong to any single one of those, since it cuts across Result, Option, Rc, and Arc. (Not to mention that I believe it only involves re-exports from core, and has importance beyond its use in std.)

@trevyn
Copy link
Contributor Author

trevyn commented Feb 23, 2021

@rustbot modify labels: T-doc

@rustbot rustbot added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Feb 23, 2021
@steveklabnik
Copy link
Member

steveklabnik commented Feb 23, 2021

To be honest, I don't really understand why we would "define" a word. It's the name of a method, that's it. There's no special definition, or any strange usage. We don't write definitions of any other method names. The definition is the documentation of the unwrap method.

@dtolnay
Copy link
Member

dtolnay commented Feb 23, 2021

I would not want to dedicate documentation to this either. The meaning in the context of Rust is 100% defined by the set of behaviors that have been given this name. When considering leveraging a preexisting name for a new behavior, the libs team evaluates how much the new behavior is in line with any old behaviors that already use the name. In my experience for those discussions a definition is not necessary, and maybe even counterproductive.

For a word that is more obscure, the place to record the way we choose to use it in standard library APIs would be the standard library dev guide at https://std-dev-guide.rust-lang.org, but I think unwrap is not one where that would be needed.

@dtolnay dtolnay added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools labels Feb 23, 2021
@trevyn
Copy link
Contributor Author

trevyn commented Feb 23, 2021

Ah, understood. Thank you for the clear explanation.

I still think that understanding “what unwrap actually means” is a meaningful hole in the user experience, but I suppose that’s a job for a community blog post or talk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants