diff options
| author | Trevor Gross <t.gross35@gmail.com> | 2025-05-27 20:28:34 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-27 20:28:34 -0400 |
| commit | da61494400b7a0fc0613d7424983a7d3c19c1afe (patch) | |
| tree | 2bf7b2585787ce7dc10e7d986b0801f995dca08c | |
| parent | f953c6dc66f092bebee0c2b446b9734812cdd70c (diff) | |
| parent | eed065958bec9402f4f12810a6ddebddb86b8433 (diff) | |
| download | rust-da61494400b7a0fc0613d7424983a7d3c19c1afe.tar.gz rust-da61494400b7a0fc0613d7424983a7d3c19c1afe.zip | |
Rollup merge of #141659 - tkr-sh:map-or-default, r=Amanieu
Add `Result::map_or_default` and `Option::map_or_default` Closes: https://github.com/rust-lang/rust/pull/138068 _This PR has been recreated because of the inactivity of the author (Cf. https://github.com/rust-lang/rust/pull/138068#issuecomment-2912412288)_
| -rw-r--r-- | library/core/src/option.rs | 30 | ||||
| -rw-r--r-- | library/core/src/result.rs | 30 |
2 files changed, 60 insertions, 0 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 1d264b26076..675556b07a8 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1253,6 +1253,36 @@ impl<T> Option<T> { } } + /// Maps an `Option<T>` to a `U` by applying function `f` to the contained + /// value if the option is [`Some`], otherwise if [`None`], returns the + /// [default value] for the type `U`. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_option_map_or_default)] + /// + /// 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 = "138099")] + pub fn map_or_default<U, F>(self, f: F) -> U + where + U: Default, + F: FnOnce(T) -> U, + { + match self { + Some(t) => f(t), + None => U::default(), + } + } + /// Transforms the `Option<T>` into a [`Result<T, E>`], 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 736ffb7d0ca..ef2da5e8fbf 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -858,6 +858,36 @@ impl<T, E> Result<T, E> { } } + /// Maps a `Result<T, E>` to a `U` by applying function `f` to the contained + /// value if the result is [`Ok`], otherwise if [`Err`], returns the + /// [default value] for the type `U`. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_option_map_or_default)] + /// + /// 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 = "138099")] + pub fn map_or_default<U, F>(self, f: F) -> U + where + U: Default, + F: FnOnce(T) -> U, + { + match self { + Ok(t) => f(t), + Err(_) => U::default(), + } + } + /// Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a /// contained [`Err`] value, leaving an [`Ok`] value untouched. /// |
