Skip to content

Commit b76dda0

Browse files
committed
initial attempt at From<& [T;N]> for Option<& [T]>
1 parent fec98b3 commit b76dda0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

library/core/src/option.rs

+16
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,22 @@ impl<T> From<T> for Option<T> {
20962096
}
20972097
}
20982098

2099+
impl<'a, T, const N: usize> From<&'a [T; N]> for Option<&'a [T]> {
2100+
/// Converts a reference to an array into a reference to an array slice.
2101+
///
2102+
/// # Examples
2103+
///
2104+
/// ```
2105+
/// let array = [1, 2, 3];
2106+
/// let option: Option<&[i32]> = Option::from(&array);
2107+
///
2108+
/// assert_eq!(option, Some(&[1, 2, 3][..]));
2109+
/// ```
2110+
fn from(slice: &'a [T; N]) -> Option<&'a [T]> {
2111+
Some(slice.as_slice())
2112+
}
2113+
}
2114+
20992115
#[stable(feature = "option_ref_from_ref_option", since = "1.30.0")]
21002116
impl<'a, T> From<&'a Option<T>> for Option<&'a T> {
21012117
/// Converts from `&Option<T>` to `Option<&T>`.

0 commit comments

Comments
 (0)