rust-lang / rust-lang/rfcs

Add methods for unwrapping Result<T, !> / Result<!, E>

Open
#1,723 31 comments 22 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

View all comments

One of the main intended uses for the ! type is to be able construct the types Result<T, !> and Result<!, E>. Right now, if you have r: Result<T, !> there are several ways to unpack it's inner T, but none of them are ideal:

  • r.unwrap(): unwrap is scary and should be avoided. This is also fragile if someone later changes the error type to something other than !.
  • r.unwrap_or_else(|e| e): Kinda confusing.
  • let Ok(val) = r;: Requires a separate statement. (Also doesn't currently work).
  • match r { Ok(v) => v, Err(e) => e }: Both long and kinda confusing.
  • match r { Ok(v) => v }: Not as nice as a method on r. (Also doesn't currently work).

The void crate adds the methods void_unwrap and void_unwrap_err to Result<T, Void> and Result<Void, T>. I think methods like these should be added to libcore as well. Perhaps we could call them always_ok() and always_err().

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the issue's examples for Result<T, !> and Result<!, E>, then compare the void crate's void_unwrap and void_unwrap_err methods. The proposed API names are still open; done would require settling the method design and adding the corresponding libcore behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.