/// Returns the contained value or a default.
///
- /// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
- /// the result of a function call, it is recommended to use `unwrap_or_else`,
- /// which is lazily evaluated.
+ /// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
+ /// the result of a function call, it is recommended to use [`unwrap_or_else`],
+ /// which is lazily evaluated.
+ ///
+ /// [`unwrap_or_else`]: #method.unwrap_or_else
///
/// # Examples
///
}
/// Applies a function to the contained value (if any),
- /// or returns a [`default`][] (if not).
- ///
- /// [`default`]: ../default/trait.Default.html#tymethod.default
+ /// or returns the provided default (if not).
///
/// # Examples
///
}
/// Applies a function to the contained value (if any),
- /// or computes a [`default`][] (if not).
- ///
- /// [`default`]: ../default/trait.Default.html#tymethod.default
+ /// or computes a default (if not).
///
/// # Examples
///
/// [`Ok(v)`] and [`None`] to [`Err(err)`].
///
/// Arguments passed to `ok_or` are eagerly evaluated; if you are passing the
- /// result of a function call, it is recommended to use `ok_or_else`, which is
+ /// result of a function call, it is recommended to use [`ok_or_else`], which is
/// lazily evaluated.
///
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
/// [`Err(err)`]: ../../std/result/enum.Result.html#variant.Err
/// [`None`]: #variant.None
/// [`Some(v)`]: #variant.Some
+ /// [`ok_or_else`]: #method.ok_or_else
///
/// # Examples
///
}
}
+ /// Returns `None` if the option is `None`, otherwise calls `predicate`
+ /// with the wrapped value and returns:
+ ///
+ /// - `Some(t)` if `predicate` returns `true` (where `t` is the wrapped
+ /// value), and
+ /// - `None` if `predicate` returns `false`.
+ ///
+ /// This function works similar to `Iterator::filter()`. You can imagine
+ /// the `Option<T>` being an iterator over one or zero elements. `filter()`
+ /// lets you decide which elements to keep.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// #![feature(option_filter)]
+ ///
+ /// fn is_even(n: &i32) -> bool {
+ /// n % 2 == 0
+ /// }
+ ///
+ /// assert_eq!(None.filter(is_even), None);
+ /// assert_eq!(Some(3).filter(is_even), None);
+ /// assert_eq!(Some(4).filter(is_even), Some(4));
+ /// ```
+ #[inline]
+ #[unstable(feature = "option_filter", issue = "45860")]
+ pub fn filter<P: FnOnce(&T) -> bool>(self, predicate: P) -> Self {
+ if let Some(x) = self {
+ if predicate(&x) {
+ return Some(x)
+ }
+ }
+ None
+ }
+
/// Returns the option if it contains a value, otherwise returns `optb`.
///
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
- /// result of a function call, it is recommended to use `or_else`, which is
+ /// result of a function call, it is recommended to use [`or_else`], which is
/// lazily evaluated.
///
+ /// [`or_else`]: #method.or_else
+ ///
/// # Examples
///
/// ```
/// Returns the contained value or a default
///
/// Consumes the `self` argument then, if [`Some`], returns the contained
- /// value, otherwise if [`None`], returns the default value for that
+ /// value, otherwise if [`None`], returns the [default value] for that
/// type.
///
/// # Examples
///
/// [`Some`]: #variant.Some
/// [`None`]: #variant.None
+ /// [default value]: ../default/trait.Default.html#tymethod.default
/// [`parse`]: ../../std/primitive.str.html#method.parse
/// [`FromStr`]: ../../std/str/trait.FromStr.html
#[inline]
}
}
+impl<T, E> Option<Result<T, E>> {
+ /// Transposes an `Option` of a `Result` into a `Result` of an `Option`.
+ ///
+ /// `None` will be mapped to `Ok(None)`.
+ /// `Some(Ok(_))` and `Some(Err(_))` will be mapped to `Ok(Some(_))` and `Err(_)`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(transpose_result)]
+ ///
+ /// #[derive(Debug, Eq, PartialEq)]
+ /// struct SomeErr;
+ ///
+ /// let x: Result<Option<i32>, SomeErr> = Ok(Some(5));
+ /// let y: Option<Result<i32, SomeErr>> = Some(Ok(5));
+ /// assert_eq!(x, y.transpose());
+ /// ```
+ #[inline]
+ #[unstable(feature = "transpose_result", issue = "47338")]
+ pub fn transpose(self) -> Result<Option<T>, E> {
+ match self {
+ Some(Ok(x)) => Ok(Some(x)),
+ Some(Err(e)) => Err(e),
+ None => Ok(None),
+ }
+ }
+}
+
// This is a separate function to reduce the code size of .expect() itself.
#[inline(never)]
#[cold]