mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Add [Option<T>; N]::transpose
This commit is contained in:
parent
702987f75b
commit
1e24104587
@ -2545,3 +2545,27 @@ impl<T> Option<Option<T>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const N: usize> [Option<T>; N] {
|
||||
/// Transposes a `[Option<T>; N]` into a `Option<[T; N]>`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(option_array_transpose)]
|
||||
/// # use std::option::Option;
|
||||
///
|
||||
/// let data = [Some(0); 1000];
|
||||
/// let data: Option<[u8; 1000]> = data.transpose();
|
||||
/// assert_eq!(data, Some([0; 1000]));
|
||||
///
|
||||
/// let data = [Some(0), None];
|
||||
/// let data: Option<[u8; 2]> = data.transpose();
|
||||
/// assert_eq!(data, None);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "option_array_transpose", issue = "130828")]
|
||||
pub fn transpose(self) -> Option<[T; N]> {
|
||||
self.try_map(core::convert::identity)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user