-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Introduce the Result::into_inner
method
#54219
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Result::into_inner
method
I don't think calling Beyond that, how often does this use case come up? You can always write I think this method needs an RFC proposal, or at the very least a discussion on internals. |
@joshtriplett we have merged larger bits without an RFC into the standard library so I don't think this needs an RFC; T-libs can just make a decision here. |
Fair enough. But I do feel that the use case needs more detailed justification, and more examples than just |
Just in the codebase of rust there is 5 possible candidates according to the result of this command: rg -USP 'match\s+.+\s+{\s+Ok\((\w+)\)\s+=>\s+\1.*\s+Err\((\w+)\)\s+=>\s+\2'
# This command searches this pattern:
# match xxx {
# Ok(a) => a,
# Err(b) => b Note that I removed unrelated results. I totaly agree that this is a much better candidate for the Another usage that I found is in my own library but it is related to something like binary search. collapsed ripgrep output
|
Hmm... one advantage of |
It looks like all but one of those cases are effectively searches similar to |
Tirage; @joshtriplett What's the current status of the review of this PR? |
I've wanted this method from time to time, though it's fairly unusual and I can't even recall the specifics. |
@nikomatsakis If the only cases where you've needed this is for |
"Fixing" |
@Centril What I'm suggesting here is that so far almost all the use cases for |
@joshtriplett Maybe what we lack is |
@Centril We can't change enum BinSearchResult {
Found(usize),
NotFound(usize),
} Or, alternatively, return |
@joshtriplett |
@Centril That's the question at hand: what other contexts or use cases? |
@joshtriplett #54219 (comment) did note some instances that were not |
I would personally think we probably don't want to add this in the sense that this is vanishingly rare to come up in practice. Almost all idiomatic usages of |
You are probably right, with the new let x = res.unwrap_or_else(identity); |
Something that I forget to match is the
All of these matches are associated with |
Some decision by T-libs would be nice :) |
@rfcbot fcp close I personally thing this is not worth adding to the standard library in that it won't necessarily get enough usage to justify having it nor is it necessarily an idiomatic patten we'd like to encourage (ok and err having the same type) |
Team member @alexcrichton has proposed to close this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
rust-lang/rfcs#2535 has been approved, and includes irrefutable Or patterns:
|
(Commenting because I've encountered a context where this would be useful that isn't a binary search.) I've repeatedly encountered instances where this method would be useful in the past year. My data analysis pipeline for some research about automated assessment of students' implementations and test suites is written in Rust. Implementations and test suites are either Each time I've "needed" |
|
The final comment period, with a disposition to close, as per the review above, is now complete. |
Ok I'm going to close this as a result of the FCP. @jswrenn it sounds like you've found a suitable enough replacement when this comes up (hopefully meaning this isn't super pressing) and we've got an RFC which may make this a bit nicer in some situations. @Amanieu while those are definitely an example of this pattern (thanks for the reminder!) I don't think that it's an argument in favor because |
we have a bunch of logical operations for Option and Result, it would seem to make sense to also have the "coalesce" logical operation for |
This method extract the value of either the
Ok
or theErr
Result
variant in the case where the variant types are the same.It is inspired by the
Either::into_inner
method.I found use cases, in particular when using
slice::binary_search
like functions which returnsResult<usize, usize>
. Note that it is always possible to apply logic on one or the other variant like so: