Rollup merge of #92530 - dtolnay:contains, r=yaahc

Move `contains` method of Option and Result lower in docs

Follow-up to #92444 trying to get the `Option` and `Result` rustdocs in better shape.

This addresses the request in https://github.com/rust-lang/rust/issues/62358#issuecomment-645676285. The `contains` methods are previously too high up in the docs on both `Option` and `Result` — stuff like `ok` and `map` and `and_then` should all be featured higher than `contains`. All of those are more ubiquitously useful than `contains`.
This commit is contained in:
Matthias Krüger 2022-01-04 21:23:10 +01:00 committed by GitHub
commit af49d81e04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 88 deletions

View File

@ -571,36 +571,6 @@ pub const fn is_none(&self) -> bool {
!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 @@ pub const fn replace(&mut self, value: 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))`.

View File

@ -563,64 +563,6 @@ pub const fn is_err(&self) -> bool {
!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 @@ pub unsafe fn unwrap_err_unchecked(self) -> 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> {