Skip to content

Add Result::map_or_default and Option::map_or_default #138068

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,35 @@ impl<T> Option<T> {
}
}

/// Maps an `Option<T>` 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
///
/// ```
/// #![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)`].
///
Expand Down
29 changes: 29 additions & 0 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,35 @@ impl<T, E> Result<T, E> {
}
}

/// Maps a `Result<T, E>` to a `U` by applying function `f` if the contained value
/// is [`Ok`], otherwise if [`Err`], returns the [default value] for the type `U`.
///
Comment on lines +833 to +835
Copy link
Contributor

@tkr-sh tkr-sh Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the conversion of the previous RFC into a tracking issue! ❤️


Just, for the documentation, I think that something like:

+ by applying function `f` to the contained value if the result is [`Ok`]
- by applying function `f` if the contained is [`Ok`]

( or something like that ) would be better because, with the previous sentence, you don't know if f takes Result<T, E>/Option<T> or T. (and same for Option)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please apply this suggestion too.

/// # 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.
///
Expand Down
Loading