+ /// Converts from `Option<T>` (or `&Option<T>`) to `Option<&T::Target>`.
+ ///
+ /// Leaves the original Option in-place, creating a new one with a reference
+ /// to the original one, additionally coercing the contents via [`Deref`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x: Option<String> = Some("hey".to_owned());
+ /// assert_eq!(x.as_deref(), Some("hey"));
+ ///
+ /// let x: Option<String> = None;
+ /// assert_eq!(x.as_deref(), None);
+ /// ```
+ #[stable(feature = "option_deref", since = "1.40.0")]
+ #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
+ pub const fn as_deref(&self) -> Option<&T::Target>
+ where
+ T: ~const Deref,
+ {
+ match self.as_ref() {
+ Some(t) => Some(t.deref()),
+ None => None,
+ }
+ }
+
+ /// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
+ ///
+ /// Leaves the original `Option` in-place, creating a new one containing a mutable reference to
+ /// the inner type's [`Deref::Target`] type.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut x: Option<String> = Some("hey".to_owned());
+ /// assert_eq!(x.as_deref_mut().map(|x| {
+ /// x.make_ascii_uppercase();
+ /// x
+ /// }), Some("HEY".to_owned().as_mut_str()));
+ /// ```
+ #[stable(feature = "option_deref", since = "1.40.0")]
+ #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
+ pub const fn as_deref_mut(&mut self) -> Option<&mut T::Target>
+ where
+ T: ~const DerefMut,
+ {
+ match self.as_mut() {
+ Some(t) => Some(t.deref_mut()),
+ None => None,
+ }
+ }
+