Skip to content

Commit bc4ce79

Browse files
committed
Added the Option::unzip() method
1 parent eaf6f46 commit bc4ce79

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

library/core/src/option.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,31 @@ impl<T> Option<T> {
13991399
}
14001400
}
14011401

1402+
impl<T, U> Option<(T, U)> {
1403+
/// Unzips an option containing a tuple of two options
1404+
///
1405+
/// If `self` is `Some((a, b))` this method returns `(Some(a), Some(b))`.
1406+
/// Otherwise, `(None, None)` is returned.
1407+
///
1408+
/// # Examples
1409+
///
1410+
/// ```
1411+
/// let x = Some((1, "hi"));
1412+
/// let y = None::<(u8, u32)>;
1413+
///
1414+
/// assert_eq!(x.unzip(), (Some(1), Some("hi")));
1415+
/// assert_eq!(y.unzip(), (None, None));
1416+
/// ```
1417+
#[inline]
1418+
#[unstable(feature = "unzip_option", issue = "none", reason = "recently added")]
1419+
pub const fn unzip(self) -> (Option<T>, Option<U>) {
1420+
match self {
1421+
Some((a, b)) => (Some(a), Some(b)),
1422+
None => (None, None),
1423+
}
1424+
}
1425+
}
1426+
14021427
impl<T: Copy> Option<&T> {
14031428
/// Maps an `Option<&T>` to an `Option<T>` by copying the contents of the
14041429
/// option.

0 commit comments

Comments
 (0)