diff options
| author | Chase Wilson <me@chasewilson.dev> | 2021-07-30 13:11:25 -0500 |
|---|---|---|
| committer | Chase Wilson <me@chasewilson.dev> | 2021-08-09 10:23:46 -0500 |
| commit | bc4ce79764f6519ee9f1469fb3fddcc3f70b8e14 (patch) | |
| tree | 7536bc7db70f597bf7db941385ce4ee4733c4436 | |
| parent | eaf6f463599df1f18da94a6965e216ea15795417 (diff) | |
| download | rust-bc4ce79764f6519ee9f1469fb3fddcc3f70b8e14.tar.gz rust-bc4ce79764f6519ee9f1469fb3fddcc3f70b8e14.zip | |
Added the `Option::unzip()` method
| -rw-r--r-- | library/core/src/option.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs index d4e9c384f93..57553865bd0 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -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. |
