Rollup merge of #60256 - ethanboxx:master, r=alexcrichton
Option::flatten This PR makes this possible. ```rust assert_eq!(Some(6), Some(Some(6)).flatten()); assert_eq!(Some(6), Some(Some(6)).into()); ```
This commit is contained in:
commit
bdf3a01065
@ -136,7 +136,7 @@
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
|
||||
use crate::{hint, mem, ops::{self, Deref}};
|
||||
use crate::{convert, hint, mem, ops::{self, Deref}};
|
||||
use crate::pin::Pin;
|
||||
|
||||
// Note that this is not a lang item per se, but it has a hidden dependency on
|
||||
@ -1413,3 +1413,33 @@ impl<T> ops::Try for Option<T> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Option<Option<T>> {
|
||||
/// Converts from `Option<Option<T>>` to `Option<T>`
|
||||
///
|
||||
/// # Examples
|
||||
/// Basic usage:
|
||||
/// ```
|
||||
/// #![feature(option_flattening)]
|
||||
/// let x: Option<Option<u32>> = Some(Some(6));
|
||||
/// assert_eq!(Some(6), x.flatten());
|
||||
///
|
||||
/// let x: Option<Option<u32>> = Some(None);
|
||||
/// assert_eq!(None, x.flatten());
|
||||
///
|
||||
/// let x: Option<Option<u32>> = None;
|
||||
/// assert_eq!(None, x.flatten());
|
||||
/// ```
|
||||
/// Flattening once only removes one level of nesting:
|
||||
/// ```
|
||||
/// #![feature(option_flattening)]
|
||||
/// let x: Option<Option<Option<u32>>> = Some(Some(Some(6)));
|
||||
/// assert_eq!(Some(Some(6)), x.flatten());
|
||||
/// assert_eq!(Some(6), x.flatten().flatten());
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "option_flattening", issue = "60258")]
|
||||
pub fn flatten(self) -> Option<T> {
|
||||
self.and_then(convert::identity)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user