Skip to content

Commit a5206f9

Browse files
committed
add Option::{zip,zip_with} methods under "option_zip" gate
This commit introduces 2 methods - `Option::zip` and `Option::zip_with` with respective signatures: - zip: `(Option<T>, Option<U>) -> Option<(T, U)>` - zip_with: `(Option<T>, Option<U>, (T, U) -> R) -> Option<R>` Both are under the feature gate "option_zip". I'm not sure about the name "zip", maybe we can find a better name for this. (I would prefer `union` for example, but this is a keyword :( ) -------------------------------------------------------------------------------- Recently in a russian rust begginers telegram chat a newbie asked (translated): > Are there any methods for these conversions: > > 1. `(Option<A>, Option<B>) -> Option<(A, B)>` > 2. `Vec<Option<T>> -> Option<Vec<T>>` > > ? While second (2.) is clearly `vec.into_iter().collect::<Option<Vec<_>>()`, the first one isn't that clear. I couldn't find anything similar in the `core` and I've come to this solution: ```rust let tuple: (Option<A>, Option<B>) = ...; let res: Option<(A, B)> = tuple.0.and_then(|a| tuple.1.map(|b| (a, b))); ``` However this solution isn't "nice" (same for just `match`/`if let`), so I thought that this functionality should be in `core`.
1 parent d939f70 commit a5206f9

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
#![feature(associated_type_bounds)]
141141
#![feature(const_type_id)]
142142
#![feature(const_caller_location)]
143+
#![feature(option_zip)]
143144
#![feature(no_niche)] // rust-lang/rust#68303
144145

145146
#[prelude_import]

src/libcore/option.rs

+59
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,65 @@ impl<T> Option<T> {
913913
pub fn replace(&mut self, value: T) -> Option<T> {
914914
mem::replace(self, Some(value))
915915
}
916+
917+
/// Zips `self` with another `Option`.
918+
///
919+
/// Returns `Some((_, _))` when both `self` and `other`
920+
/// are `Some(_)`, otherwise return `None`.
921+
///
922+
/// # Examples
923+
///
924+
/// ```
925+
/// #![feature(option_zip)]
926+
/// let x = Some(1);
927+
/// let y = Some("hi");
928+
/// let z = None::<u8>;
929+
///
930+
/// assert_eq!(x.zip(y), Some((1, "hi")));
931+
/// assert_eq!(x.zip(z), None);
932+
/// ```
933+
#[inline]
934+
#[unstable(feature = "option_zip", issue = "none")]
935+
pub fn zip<U>(self, other: Option<U>) -> Option<(T, U)> {
936+
self.zip_with(other, |a, b| (a, b))
937+
}
938+
939+
/// Zips `self` and another `Option` with function `f`.
940+
///
941+
/// Returns `Some(_)` when both `self` and `other`
942+
/// are `Some(_)`, otherwise return `None`.
943+
///
944+
/// # Examples
945+
///
946+
/// ```
947+
/// #![feature(option_zip)]
948+
///
949+
/// #[derive(Debug, PartialEq)]
950+
/// struct Point {
951+
/// x: f64,
952+
/// y: f64,
953+
/// }
954+
///
955+
/// impl Point {
956+
/// fn new(x: f64, y: f64) -> Self {
957+
/// Self { x, y }
958+
/// }
959+
/// }
960+
///
961+
/// let x = Some(17.);
962+
/// let y = Some(42.);
963+
///
964+
/// assert_eq!(x.zip_with(y, Point::new), Some(Point { x: 17., y: 42. }));
965+
/// assert_eq!(x.zip_with(None, Point::new), None);
966+
/// ```
967+
#[inline]
968+
#[unstable(feature = "option_zip", issue = "none")]
969+
pub fn zip_with<U, F, R>(self, other: Option<U>, f: F) -> Option<R>
970+
where
971+
F: FnOnce(T, U) -> R,
972+
{
973+
Some(f(self?, other?))
974+
}
916975
}
917976

918977
impl<T: Copy> Option<&T> {

0 commit comments

Comments
 (0)