Skip to content
Closed
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