-
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
Add a Result::into_ok_or_err
method to extract a T
from Result<T, T>
#80572
Changes from all commits
f688bee
7d30366
2711b01
404da0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1276,6 +1276,41 @@ impl<T, E> Result<Result<T, E>, E> { | |||||
} | ||||||
} | ||||||
|
||||||
impl<T> Result<T, T> { | ||||||
/// Returns the [`Ok`] value if `self` is `Ok`, and the [`Err`] value if | ||||||
/// `self` is `Err`. | ||||||
/// | ||||||
/// In other words, this function returns the value (the `T`) of a | ||||||
/// `Result<T, T>`, regardless of whether or not that result is `Ok` or | ||||||
/// `Err`. | ||||||
/// | ||||||
/// This can be useful in conjunction with APIs such as | ||||||
/// [`Atomic*::compare_exchange`], or [`slice::binary_search`][binary_search], but only in | ||||||
/// cases where you don't care if the result was `Ok` or not. | ||||||
/// | ||||||
/// [`Atomic*::compare_exchange`]: crate::sync::atomic::AtomicBool::compare_exchange | ||||||
/// [binary_search]: ../../std/primitive.slice.html#method.binary_search | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that:
That said, I'm also okay with getting rid of these links entirely if that's desirable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can now use intra-doc links here!
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, better yet you can change the part above that has the link to just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, the bootstrap bump hasn't happened yet. Intra-doc pointers landed in #80181, which is in 1.51, which is still in nightly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dang, I always forget about the bootstrap bump :( |
||||||
/// | ||||||
/// # Examples | ||||||
/// | ||||||
/// ``` | ||||||
/// #![feature(result_into_ok_or_err)] | ||||||
/// let ok: Result<u32, u32> = Ok(3); | ||||||
/// let err: Result<u32, u32> = Err(4); | ||||||
/// | ||||||
/// assert_eq!(ok.into_ok_or_err(), 3); | ||||||
/// assert_eq!(err.into_ok_or_err(), 4); | ||||||
/// ``` | ||||||
#[inline] | ||||||
#[unstable(feature = "result_into_ok_or_err", reason = "newly added", issue = "82223")] | ||||||
pub const fn into_ok_or_err(self) -> T { | ||||||
match self { | ||||||
Ok(v) => v, | ||||||
Err(v) => v, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// This is a separate function to reduce the code size of the methods | ||||||
#[inline(never)] | ||||||
#[cold] | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Binary search is the primary reason I've wanted similar, but I've also seen people want to use it in procedural macros where you use a
TokenStream
for success and failure cases.This seems reasonable to add as an unstable feature.