Added the Option::unzip() method

This commit is contained in:
Chase Wilson 2021-07-30 13:11:25 -05:00
parent eaf6f46359
commit bc4ce79764
No known key found for this signature in database
GPG Key ID: 190140C6730EB7E1

View File

@ -1399,6 +1399,31 @@ impl<T> Option<T> {
}
}
impl<T, U> Option<(T, U)> {
/// Unzips an option containing a tuple of two options
///
/// If `self` is `Some((a, b))` this method returns `(Some(a), Some(b))`.
/// Otherwise, `(None, None)` is returned.
///
/// # Examples
///
/// ```
/// let x = Some((1, "hi"));
/// let y = None::<(u8, u32)>;
///
/// assert_eq!(x.unzip(), (Some(1), Some("hi")));
/// assert_eq!(y.unzip(), (None, None));
/// ```
#[inline]
#[unstable(feature = "unzip_option", issue = "none", reason = "recently added")]
pub const fn unzip(self) -> (Option<T>, Option<U>) {
match self {
Some((a, b)) => (Some(a), Some(b)),
None => (None, None),
}
}
}
impl<T: Copy> Option<&T> {
/// Maps an `Option<&T>` to an `Option<T>` by copying the contents of the
/// option.