From 2de6c35d466c4114ef3f285bff2be57d156835e5 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 12 Nov 2019 03:08:19 +0000 Subject: [PATCH 1/2] make Result::map_or_else consistent with Option::map_or_else --- src/libcore/result.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index ed40a5f31d9bd..3ac43958c059a 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -540,8 +540,11 @@ impl Result { /// ``` #[inline] #[unstable(feature = "result_map_or_else", issue = "53268")] - pub fn map_or_else U, F: FnOnce(E) -> U>(self, fallback: F, map: M) -> U { - self.map(map).unwrap_or_else(fallback) + pub fn map_or_else U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U { + match self { + Ok(t) => f(t), + Err(e) => default(e), + } } /// Maps a `Result` to `Result` by applying a function to a From c06a8ea727c720ed47b3f54192e92c449252d231 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 12 Nov 2019 03:09:10 +0000 Subject: [PATCH 2/2] stabilize Result::map_or_else --- src/libcore/result.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 3ac43958c059a..fd9cb12996b40 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -529,7 +529,6 @@ impl Result { /// Basic usage: /// /// ``` - /// #![feature(result_map_or_else)] /// let k = 21; /// /// let x : Result<_, &str> = Ok("foo"); @@ -539,7 +538,7 @@ impl Result { /// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42); /// ``` #[inline] - #[unstable(feature = "result_map_or_else", issue = "53268")] + #[stable(feature = "result_map_or_else", since = "1.41.0")] pub fn map_or_else U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U { match self { Ok(t) => f(t),