Skip to content

Add Result::map_or_default and Option::map_or_default #141659

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

Merged
merged 1 commit into from
May 28, 2025
Merged
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
30 changes: 30 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`].
///
Expand Down
30 changes: 30 additions & 0 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// value if the result is [`Ok`], otherwise if [`Err`], returns the
/// value if the result is [`Ok`], otherwise if [`Err`] returns the

Copy link
Contributor Author

@tkr-sh tkr-sh May 27, 2025

Choose a reason for hiding this comment

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

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