From 86599e0c749760342677f6ba195a5cbf2a9707c1 Mon Sep 17 00:00:00 2001 From: Faizaan Pervaiz Date: Wed, 5 Mar 2025 17:51:55 +0000 Subject: [PATCH 1/3] Add `Result::map_or_default` and `Option::map_or_default` --- library/core/src/option.rs | 27 +++++++++++++++++++++++++++ library/core/src/result.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index a9f06b92ad5dd..a4ff81ef79105 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1226,6 +1226,33 @@ impl Option { } } + /// Maps an `Option` to a `U` by applying function `f` if the contained value + /// is [`Some`], otherwise if [`None`], returns the [default value] for the type `U`. + /// + /// # Examples + /// + /// ``` + /// let x: Option<&str> = Some("hi"); + /// let y: Option<&str> = None; + /// + /// assert_eq!(x.map_or_default(|x| x.len()), 2); + /// assert_eq!(y.map_or_default(|y| y.len()), 0); + /// ``` + /// + /// [default value]: Default::default + #[inline] + #[unstable(feature = "result_option_map_or_default", issue = "none")] + pub fn map_or_default(self, f: F) -> U + where + U: Default, + F: FnOnce(T) -> U, + { + match self { + Some(t) => f(t), + None => U::default(), + } + } + /// Transforms the `Option` into a [`Result`], mapping [`Some(v)`] to /// [`Ok(v)`] and [`None`] to [`Err(err)`]. /// diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 92b5cba153166..742fd5694e594 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -830,6 +830,33 @@ impl Result { } } + /// Maps a `Result` to a `U` by applying function `f` if the contained value + /// is [`Ok`], otherwise if [`Err`], returns the [default value] for the type `U`. + /// + /// # Examples + /// + /// ``` + /// let x: Result<_, &str> = Ok("foo"); + /// let y: Result<&str, _> = Err("bar"); + /// + /// assert_eq!(x.map_or_default(|x| x.len()), 3); + /// assert_eq!(y.map_or_default(|y| y.len()), 0); + /// ``` + /// + /// [default value]: Default::default + #[inline] + #[unstable(feature = "result_option_map_or_default", issue = "none")] + pub fn map_or_default(self, f: F) -> U + where + U: Default, + F: FnOnce(T) -> U, + { + match self { + Ok(t) => f(t), + Err(_) => U::default(), + } + } + /// Maps a `Result` to `Result` by applying a function to a /// contained [`Err`] value, leaving an [`Ok`] value untouched. /// From bfbc296e0048d93cf962cb82718fa80a6c65963c Mon Sep 17 00:00:00 2001 From: Faizaan Pervaiz Date: Wed, 5 Mar 2025 18:35:22 +0000 Subject: [PATCH 2/3] missing doc feature --- library/core/src/option.rs | 2 ++ library/core/src/result.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index a4ff81ef79105..d12966183b7a2 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1232,6 +1232,8 @@ impl Option { /// # Examples /// /// ``` + /// #![feature(result_option_map_or_default)] + /// /// let x: Option<&str> = Some("hi"); /// let y: Option<&str> = None; /// diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 742fd5694e594..cec3c02d65f2e 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -836,6 +836,8 @@ impl Result { /// # Examples /// /// ``` + /// #![feature(result_option_map_or_default)] + /// /// let x: Result<_, &str> = Ok("foo"); /// let y: Result<&str, _> = Err("bar"); /// From 278070be3e2aa31d23b60c1287489808ffab8220 Mon Sep 17 00:00:00 2001 From: Faizaan Pervaiz Date: Thu, 6 Mar 2025 13:24:06 +0000 Subject: [PATCH 3/3] add tracking issue --- library/core/src/option.rs | 2 +- library/core/src/result.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index d12966183b7a2..3d7ec570f1016 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1243,7 +1243,7 @@ impl Option { /// /// [default value]: Default::default #[inline] - #[unstable(feature = "result_option_map_or_default", issue = "none")] + #[unstable(feature = "result_option_map_or_default", issue = "138099")] pub fn map_or_default(self, f: F) -> U where U: Default, diff --git a/library/core/src/result.rs b/library/core/src/result.rs index cec3c02d65f2e..63f3fe57ab19b 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -847,7 +847,7 @@ impl Result { /// /// [default value]: Default::default #[inline] - #[unstable(feature = "result_option_map_or_default", issue = "none")] + #[unstable(feature = "result_option_map_or_default", issue = "138099")] pub fn map_or_default(self, f: F) -> U where U: Default,