diff options
| author | David Tolnay <dtolnay@gmail.com> | 2022-01-03 10:46:15 -0800 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2022-01-03 10:46:15 -0800 |
| commit | 7dec41a2367b995f3067e2e5cb644cff5ba99cfc (patch) | |
| tree | 50d32498ee67c5e36ae6db48896430939ae2f0c5 | |
| parent | ddabe0775c5f5b551d5eb54e3c4366fb8bec0c92 (diff) | |
| download | rust-7dec41a2367b995f3067e2e5cb644cff5ba99cfc.tar.gz rust-7dec41a2367b995f3067e2e5cb644cff5ba99cfc.zip | |
Move `contains` method of Option and Result lower in docs
| -rw-r--r-- | library/core/src/option.rs | 60 | ||||
| -rw-r--r-- | library/core/src/result.rs | 120 |
2 files changed, 92 insertions, 88 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 0022df4f65f..1ec119a71e4 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -571,36 +571,6 @@ impl<T> Option<T> { !self.is_some() } - /// Returns `true` if the option is a [`Some`] value containing the given value. - /// - /// # Examples - /// - /// ``` - /// #![feature(option_result_contains)] - /// - /// let x: Option<u32> = Some(2); - /// assert_eq!(x.contains(&2), true); - /// - /// let x: Option<u32> = Some(3); - /// assert_eq!(x.contains(&2), false); - /// - /// let x: Option<u32> = None; - /// assert_eq!(x.contains(&2), false); - /// ``` - #[must_use] - #[inline] - #[unstable(feature = "option_result_contains", issue = "62358")] - #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")] - pub const fn contains<U>(&self, x: &U) -> bool - where - U: ~const PartialEq<T>, - { - match self { - Some(y) => x.eq(y), - None => false, - } - } - ///////////////////////////////////////////////////////////////////////// // Adapter for working with references ///////////////////////////////////////////////////////////////////////// @@ -1573,6 +1543,36 @@ impl<T> Option<T> { mem::replace(self, Some(value)) } + /// Returns `true` if the option is a [`Some`] value containing the given value. + /// + /// # Examples + /// + /// ``` + /// #![feature(option_result_contains)] + /// + /// let x: Option<u32> = Some(2); + /// assert_eq!(x.contains(&2), true); + /// + /// let x: Option<u32> = Some(3); + /// assert_eq!(x.contains(&2), false); + /// + /// let x: Option<u32> = None; + /// assert_eq!(x.contains(&2), false); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "option_result_contains", issue = "62358")] + #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")] + pub const fn contains<U>(&self, x: &U) -> bool + where + U: ~const PartialEq<T>, + { + match self { + Some(y) => x.eq(y), + None => false, + } + } + /// Zips `self` with another `Option`. /// /// If `self` is `Some(s)` and `other` is `Some(o)`, this method returns `Some((s, o))`. diff --git a/library/core/src/result.rs b/library/core/src/result.rs index f46632e7a8d..504a01813ac 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -563,64 +563,6 @@ impl<T, E> Result<T, E> { !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 ///////////////////////////////////////////////////////////////////////// @@ -1491,6 +1433,68 @@ impl<T, E> Result<T, E> { Err(e) => e, } } + + ///////////////////////////////////////////////////////////////////////// + // Misc or niche + ///////////////////////////////////////////////////////////////////////// + + /// 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, + } + } } impl<T, E> Result<&T, E> { |
