diff --git a/library/core/src/option.rs b/library/core/src/option.rs index e6312b8b2d947..f6997fb962ebd 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -690,6 +690,76 @@ impl Option { } } + ///////////////////////////////////////////////////////////////////////// + // Extracting slices + ///////////////////////////////////////////////////////////////////////// + + /// Extracts a slice that's empty if the option is a [`None`] value or + /// length one if the option is a [`Some`] value. + /// + /// Note that the slice extracted from a [`None`] value does not + /// necessarily contain an internal pointer to anything associated with + /// the option. + /// + /// # Examples + /// + /// ``` + /// #![feature(option_as_slice)] + /// + /// let x: Option = Some(7); + /// assert_eq!(x.as_slice(), [7]); + /// + /// let x: Option = None; + /// assert_eq!(x.as_slice(), []); + /// ``` + #[inline] + #[unstable(feature = "option_as_slice", issue = "none")] + pub const fn as_slice(&self) -> &[T] { + match *self { + Some(ref x) => core::slice::from_ref(x), + None => &[], + } + } + + /// Extracts a mutable slice that's empty if the option is a [`None`] + /// value or length one if the option is a [`Some`] value. + /// + /// Note that the slice extracted from a [`None`] value does not + /// necessarily contain an internal pointer to anything associated with + /// the option. + /// + /// # Examples + /// + /// ``` + /// #![feature(option_as_slice)] + /// + /// let mut x: Option = Some(2); + /// let x_as_slice = x.as_mut_slice(); + /// assert_eq!(x_as_slice, [2]); + /// + /// if !x_as_slice.is_empty() { + /// x_as_slice[0] = 42; + /// } + /// assert_eq!(x, Some(42)); + /// + /// let mut x: Option = None; + /// let x_as_slice = x.as_mut_slice(); + /// assert_eq!(x_as_slice, []); + /// + /// if !x_as_slice.is_empty() { + /// x_as_slice[0] = 42; + /// } + /// assert_eq!(x, None); + /// ``` + #[inline] + #[unstable(feature = "option_as_slice", issue = "none")] + pub const fn as_mut_slice(&mut self) -> &mut [T] { + match *self { + Some(ref mut x) => core::slice::from_mut(x), + None => &mut [], + } + } + ///////////////////////////////////////////////////////////////////////// // Getting to contained values /////////////////////////////////////////////////////////////////////////