!self.is_ok()
}
- /// Returns `true` if the result is an [`Ok`] value containing the given value.
- ///
- /// # Examples
- ///
- /// ```
- /// #![feature(option_result_contains)]
- ///
- /// let x: Result<u32, &str> = Ok(2);
- /// assert_eq!(x.contains(&2), true);
- ///
- /// let x: Result<u32, &str> = Ok(3);
- /// assert_eq!(x.contains(&2), false);
- ///
- /// let x: Result<u32, &str> = Err("Some error message");
- /// assert_eq!(x.contains(&2), false);
- /// ```
- #[must_use]
- #[inline]
- #[unstable(feature = "option_result_contains", issue = "62358")]
- pub fn contains<U>(&self, x: &U) -> bool
- where
- U: PartialEq<T>,
- {
- match self {
- Ok(y) => x == y,
- Err(_) => false,
- }
- }
-
- /// Returns `true` if the result is an [`Err`] value containing the given value.
- ///
- /// # Examples
- ///
- /// ```
- /// #![feature(result_contains_err)]
- ///
- /// let x: Result<u32, &str> = Ok(2);
- /// assert_eq!(x.contains_err(&"Some error message"), false);
- ///
- /// let x: Result<u32, &str> = Err("Some error message");
- /// assert_eq!(x.contains_err(&"Some error message"), true);
- ///
- /// let x: Result<u32, &str> = Err("Some other error message");
- /// assert_eq!(x.contains_err(&"Some error message"), false);
- /// ```
- #[must_use]
- #[inline]
- #[unstable(feature = "result_contains_err", issue = "62358")]
- pub fn contains_err<F>(&self, f: &F) -> bool
- where
- F: PartialEq<E>,
- {
- match self {
- Ok(_) => false,
- Err(e) => f == e,
- }
- }
-
/////////////////////////////////////////////////////////////////////////
// Adapter for each variant
/////////////////////////////////////////////////////////////////////////
Err(e) => e,
}
}
-}
-impl<T: Copy, E> Result<&T, E> {
- /// Maps a `Result<&T, E>` to a `Result<T, E>` by copying the contents of the
- /// `Ok` part.
+ /////////////////////////////////////////////////////////////////////////
+ // Misc or niche
+ /////////////////////////////////////////////////////////////////////////
+
+ /// Returns `true` if the result is an [`Ok`] value containing the given value.
///
/// # Examples
///
/// ```
- /// #![feature(result_copied)]
- /// let val = 12;
- /// let x: Result<&i32, i32> = Ok(&val);
- /// assert_eq!(x, Ok(&12));
- /// let copied = x.copied();
- /// assert_eq!(copied, Ok(12));
+ /// #![feature(option_result_contains)]
+ ///
+ /// let x: Result<u32, &str> = Ok(2);
+ /// assert_eq!(x.contains(&2), true);
+ ///
+ /// let x: Result<u32, &str> = Ok(3);
+ /// assert_eq!(x.contains(&2), false);
+ ///
+ /// let x: Result<u32, &str> = Err("Some error message");
+ /// assert_eq!(x.contains(&2), false);
/// ```
- #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
- pub fn copied(self) -> Result<T, E> {
- self.map(|&t| t)
+ #[must_use]
+ #[inline]
+ #[unstable(feature = "option_result_contains", issue = "62358")]
+ pub fn contains<U>(&self, x: &U) -> bool
+ where
+ U: PartialEq<T>,
+ {
+ match self {
+ Ok(y) => x == y,
+ Err(_) => false,
+ }
+ }
+
+ /// Returns `true` if the result is an [`Err`] value containing the given value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(result_contains_err)]
+ ///
+ /// let x: Result<u32, &str> = Ok(2);
+ /// assert_eq!(x.contains_err(&"Some error message"), false);
+ ///
+ /// let x: Result<u32, &str> = Err("Some error message");
+ /// assert_eq!(x.contains_err(&"Some error message"), true);
+ ///
+ /// let x: Result<u32, &str> = Err("Some other error message");
+ /// assert_eq!(x.contains_err(&"Some error message"), false);
+ /// ```
+ #[must_use]
+ #[inline]
+ #[unstable(feature = "result_contains_err", issue = "62358")]
+ pub fn contains_err<F>(&self, f: &F) -> bool
+ where
+ F: PartialEq<E>,
+ {
+ match self {
+ Ok(_) => false,
+ Err(e) => f == e,
+ }
}
}
-impl<T: Copy, E> Result<&mut T, E> {
- /// Maps a `Result<&mut T, E>` to a `Result<T, E>` by copying the contents of the
+impl<T, E> Result<&T, E> {
+ /// Maps a `Result<&T, E>` to a `Result<T, E>` by copying the contents of the
/// `Ok` part.
///
/// # Examples
///
/// ```
- /// #![feature(result_copied)]
- /// let mut val = 12;
- /// let x: Result<&mut i32, i32> = Ok(&mut val);
- /// assert_eq!(x, Ok(&mut 12));
+ /// let val = 12;
+ /// let x: Result<&i32, i32> = Ok(&val);
+ /// assert_eq!(x, Ok(&12));
/// let copied = x.copied();
/// assert_eq!(copied, Ok(12));
/// ```
- #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
- pub fn copied(self) -> Result<T, E> {
- self.map(|&mut t| t)
+ #[inline]
+ #[stable(feature = "result_copied", since = "1.59.0")]
+ pub fn copied(self) -> Result<T, E>
+ where
+ T: Copy,
+ {
+ self.map(|&t| t)
}
-}
-impl<T: Clone, E> Result<&T, E> {
/// Maps a `Result<&T, E>` to a `Result<T, E>` by cloning the contents of the
/// `Ok` part.
///
/// # Examples
///
/// ```
- /// #![feature(result_cloned)]
/// let val = 12;
/// let x: Result<&i32, i32> = Ok(&val);
/// assert_eq!(x, Ok(&12));
/// let cloned = x.cloned();
/// assert_eq!(cloned, Ok(12));
/// ```
- #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
- pub fn cloned(self) -> Result<T, E> {
+ #[inline]
+ #[stable(feature = "result_cloned", since = "1.59.0")]
+ pub fn cloned(self) -> Result<T, E>
+ where
+ T: Clone,
+ {
self.map(|t| t.clone())
}
}
-impl<T: Clone, E> Result<&mut T, E> {
+impl<T, E> Result<&mut T, E> {
+ /// Maps a `Result<&mut T, E>` to a `Result<T, E>` by copying the contents of the
+ /// `Ok` part.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut val = 12;
+ /// let x: Result<&mut i32, i32> = Ok(&mut val);
+ /// assert_eq!(x, Ok(&mut 12));
+ /// let copied = x.copied();
+ /// assert_eq!(copied, Ok(12));
+ /// ```
+ #[inline]
+ #[stable(feature = "result_copied", since = "1.59.0")]
+ pub fn copied(self) -> Result<T, E>
+ where
+ T: Copy,
+ {
+ self.map(|&mut t| t)
+ }
+
/// Maps a `Result<&mut T, E>` to a `Result<T, E>` by cloning the contents of the
/// `Ok` part.
///
/// # Examples
///
/// ```
- /// #![feature(result_cloned)]
/// let mut val = 12;
/// let x: Result<&mut i32, i32> = Ok(&mut val);
/// assert_eq!(x, Ok(&mut 12));
/// let cloned = x.cloned();
/// assert_eq!(cloned, Ok(12));
/// ```
- #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
- pub fn cloned(self) -> Result<T, E> {
+ #[inline]
+ #[stable(feature = "result_cloned", since = "1.59.0")]
+ pub fn cloned(self) -> Result<T, E>
+ where
+ T: Clone,
+ {
self.map(|t| t.clone())
}
}