Skip to content

Commit 0580e66

Browse files
committed
Add [Option<T>; N]::transpose
1 parent ba6158c commit 0580e66

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

core/src/option.rs

+24
Original file line numberDiff line numberDiff line change
@@ -2545,3 +2545,27 @@ impl<T> Option<Option<T>> {
25452545
}
25462546
}
25472547
}
2548+
2549+
impl<T, const N: usize> [Option<T>; N] {
2550+
/// Transposes a `[Option<T>; N]` into a `Option<[T; N]>`.
2551+
///
2552+
/// # Examples
2553+
///
2554+
/// ```
2555+
/// #![feature(option_array_transpose)]
2556+
/// # use std::option::Option;
2557+
///
2558+
/// let data = [Some(0); 1000];
2559+
/// let data: Option<[u8; 1000]> = data.transpose();
2560+
/// assert_eq!(data, Some([0; 1000]));
2561+
///
2562+
/// let data = [Some(0), None];
2563+
/// let data: Option<[u8; 2]> = data.transpose();
2564+
/// assert_eq!(data, None);
2565+
/// ```
2566+
#[inline]
2567+
#[unstable(feature = "option_array_transpose", issue = "130828")]
2568+
pub fn transpose(self) -> Option<[T; N]> {
2569+
self.try_map(core::convert::identity)
2570+
}
2571+
}

0 commit comments

Comments
 (0)