Skip to content

API: Add err_or and err_or_else to Option #80190

Closed
@LunarLambda

Description

@LunarLambda

sometimes, particularly when interfacing with C libraries which provide their own errno-like API (GetLastError, SDL_GetError, etc), Rust bindings may opt to return Option<ErrorType> to model the absence of an error.

This could then be 'upgraded' to a Result<T, E>, in a manner similar to ok_or:

fn example() -> Result<(), SomeError> {
    get_some_error().err_or(())
}

pros

cons

  • name looks like error, might cause confusion
  • not extremely common use case? (haven't checked discussion on unwrap_none yet)

Activity

added
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Dec 19, 2020
joshtriplett

joshtriplett commented on Aug 2, 2021

@joshtriplett
Member

What's the advantage of returning Option<ErrorType> rather than Result<(), ErrorType>? Semantically, the latter feels more accurate. And both are capable of handling niches in the same way.

m-ou-se

m-ou-se commented on Aug 11, 2021

@m-ou-se
Member

We discussed this briefly in the library api team meeting last week, and we'd like to see some concrete use cases. Do you have examples from some Rust crates where this Option to Result::Err conversion would be used?

added
PG-error-handlingProject group: Error handling (https://github.com/rust-lang/project-error-handling)
on Sep 27, 2021
yaahc

yaahc commented on Sep 27, 2021

@yaahc
Member

Closing this for now due to lack of responses with concrete use cases. Feel free to reopen in the future if you have a use case where you feel this API would be justified.

NathanielB123

NathanielB123 commented on Jan 2, 2023

@NathanielB123

I think I have a concrete use-case for this. I am using the insert function in HashMap<K, V> which returns Option<V> if there is already an item in the hash map. In my code, and I would argue probably most of the time, an item already being present in the hash map implies some error has occurred, and so I want to return Result<SuccessType, V>. err_or_else would be perfect for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-error-handlingArea: Error handlingA-result-optionArea: Result and Option combinatorsC-feature-requestCategory: A feature request, i.e: not implemented / a PR.PG-error-handlingProject group: Error handling (https://github.com/rust-lang/project-error-handling)T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @joshtriplett@m-ou-se@jonas-schievink@yaahc@NathanielB123

        Issue actions

          API: Add `err_or` and `err_or_else` to `Option` · Issue #80190 · rust-lang/rust