Auto merge of #66322 - lzutao:consistent-result-map_or_else, r=dtolnay
Stabilize Result::map_or_else Stabilized this API: ```rust impl<T, E> Result<T, E> { pub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U { match self { Ok(t) => f(t), Err(e) => default(e), } } } ``` Closes #53268 r? @SimonSapin
This commit is contained in:
commit
7d761fe046
@ -551,7 +551,6 @@ impl<T, E> Result<T, E> {
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(result_map_or_else)]
|
||||
/// let k = 21;
|
||||
///
|
||||
/// let x : Result<_, &str> = Ok("foo");
|
||||
@ -561,9 +560,12 @@ impl<T, E> Result<T, E> {
|
||||
/// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "result_map_or_else", issue = "53268")]
|
||||
pub fn map_or_else<U, M: FnOnce(T) -> U, F: FnOnce(E) -> U>(self, fallback: F, map: M) -> U {
|
||||
self.map(map).unwrap_or_else(fallback)
|
||||
#[stable(feature = "result_map_or_else", since = "1.41.0")]
|
||||
pub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
|
||||
match self {
|
||||
Ok(t) => f(t),
|
||||
Err(e) => default(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a
|
||||
|
Loading…
x
Reference in New Issue
Block a user