Skip to content

add Option::{err_or,err_or_else} methods under option_err_or gate #73040

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from

Conversation

WaffleLapkin
Copy link
Member

This PR adds 2 methods to Option<T>:

  • err_or (similar to ok_or, signature is fn<T, O>(Option<T>, O) -> Result<T, O>)
  • err_or_else (similar to ok_or_else, signature is fn<T, O>(Option<T>, impl FnOnce() -> O) -> Result<T, O>)

Both under option_err_or feature gate.


It seems unintuitive to have ok_or and ok_or_else, but not err* versions. This PR fills this gap in Result<->Option transformations.

Those methods may be less useful, then their ok* versions, however, need for them comes every now and then.


I'm curious if the name err_or is ok? Isn't It too similar to error?

This commit adds 2 methods to `Option<T>`:
- `err_or` (similar to `ok_or`, signature is `fn<T, O>(Option<T>, O) -> Result<T, O>`)
- `err_or_else` (similar to `ok_or_else`, signature is `fn<T, O>(Option<T>, impl FnOnce() -> O) -> Result<T, O>`)
Both under `option_err_or` feature gate.
@rust-highfive
Copy link
Contributor

r? @joshtriplett

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 5, 2020
@Elinvynia Elinvynia added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 2, 2020
@crlf0710
Copy link
Member

crlf0710 commented Jul 3, 2020

Reassigning to t-libs.
r? @dtolnay

@rust-highfive rust-highfive assigned dtolnay and unassigned joshtriplett Jul 3, 2020
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I think I would prefer not to add this set of methods. As you mentioned, these are pretty rarely applicable. Instead of x.err_or(0) I would recommend that people write the same thing using existing methods as x.map_or(Ok(0), Err) or using match.

@dtolnay dtolnay closed this Jul 3, 2020
@WaffleLapkin WaffleLapkin deleted the option_err_or branch July 3, 2020 06:24
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants