Skip to content

Commit af49d81

Browse files
authored
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 #62358 (comment). 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`.
2 parents e1a7743 + 7dec41a commit af49d81

File tree

2 files changed

+92
-88
lines changed

2 files changed

+92
-88
lines changed

Diff for: library/core/src/option.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -571,36 +571,6 @@ impl<T> Option<T> {
571571
!self.is_some()
572572
}
573573

574-
/// Returns `true` if the option is a [`Some`] value containing the given value.
575-
///
576-
/// # Examples
577-
///
578-
/// ```
579-
/// #![feature(option_result_contains)]
580-
///
581-
/// let x: Option<u32> = Some(2);
582-
/// assert_eq!(x.contains(&2), true);
583-
///
584-
/// let x: Option<u32> = Some(3);
585-
/// assert_eq!(x.contains(&2), false);
586-
///
587-
/// let x: Option<u32> = None;
588-
/// assert_eq!(x.contains(&2), false);
589-
/// ```
590-
#[must_use]
591-
#[inline]
592-
#[unstable(feature = "option_result_contains", issue = "62358")]
593-
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
594-
pub const fn contains<U>(&self, x: &U) -> bool
595-
where
596-
U: ~const PartialEq<T>,
597-
{
598-
match self {
599-
Some(y) => x.eq(y),
600-
None => false,
601-
}
602-
}
603-
604574
/////////////////////////////////////////////////////////////////////////
605575
// Adapter for working with references
606576
/////////////////////////////////////////////////////////////////////////
@@ -1573,6 +1543,36 @@ impl<T> Option<T> {
15731543
mem::replace(self, Some(value))
15741544
}
15751545

1546+
/// Returns `true` if the option is a [`Some`] value containing the given value.
1547+
///
1548+
/// # Examples
1549+
///
1550+
/// ```
1551+
/// #![feature(option_result_contains)]
1552+
///
1553+
/// let x: Option<u32> = Some(2);
1554+
/// assert_eq!(x.contains(&2), true);
1555+
///
1556+
/// let x: Option<u32> = Some(3);
1557+
/// assert_eq!(x.contains(&2), false);
1558+
///
1559+
/// let x: Option<u32> = None;
1560+
/// assert_eq!(x.contains(&2), false);
1561+
/// ```
1562+
#[must_use]
1563+
#[inline]
1564+
#[unstable(feature = "option_result_contains", issue = "62358")]
1565+
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
1566+
pub const fn contains<U>(&self, x: &U) -> bool
1567+
where
1568+
U: ~const PartialEq<T>,
1569+
{
1570+
match self {
1571+
Some(y) => x.eq(y),
1572+
None => false,
1573+
}
1574+
}
1575+
15761576
/// Zips `self` with another `Option`.
15771577
///
15781578
/// If `self` is `Some(s)` and `other` is `Some(o)`, this method returns `Some((s, o))`.

Diff for: library/core/src/result.rs

+62-58
Original file line numberDiff line numberDiff line change
@@ -563,64 +563,6 @@ impl<T, E> Result<T, E> {
563563
!self.is_ok()
564564
}
565565

566-
/// Returns `true` if the result is an [`Ok`] value containing the given value.
567-
///
568-
/// # Examples
569-
///
570-
/// ```
571-
/// #![feature(option_result_contains)]
572-
///
573-
/// let x: Result<u32, &str> = Ok(2);
574-
/// assert_eq!(x.contains(&2), true);
575-
///
576-
/// let x: Result<u32, &str> = Ok(3);
577-
/// assert_eq!(x.contains(&2), false);
578-
///
579-
/// let x: Result<u32, &str> = Err("Some error message");
580-
/// assert_eq!(x.contains(&2), false);
581-
/// ```
582-
#[must_use]
583-
#[inline]
584-
#[unstable(feature = "option_result_contains", issue = "62358")]
585-
pub fn contains<U>(&self, x: &U) -> bool
586-
where
587-
U: PartialEq<T>,
588-
{
589-
match self {
590-
Ok(y) => x == y,
591-
Err(_) => false,
592-
}
593-
}
594-
595-
/// Returns `true` if the result is an [`Err`] value containing the given value.
596-
///
597-
/// # Examples
598-
///
599-
/// ```
600-
/// #![feature(result_contains_err)]
601-
///
602-
/// let x: Result<u32, &str> = Ok(2);
603-
/// assert_eq!(x.contains_err(&"Some error message"), false);
604-
///
605-
/// let x: Result<u32, &str> = Err("Some error message");
606-
/// assert_eq!(x.contains_err(&"Some error message"), true);
607-
///
608-
/// let x: Result<u32, &str> = Err("Some other error message");
609-
/// assert_eq!(x.contains_err(&"Some error message"), false);
610-
/// ```
611-
#[must_use]
612-
#[inline]
613-
#[unstable(feature = "result_contains_err", issue = "62358")]
614-
pub fn contains_err<F>(&self, f: &F) -> bool
615-
where
616-
F: PartialEq<E>,
617-
{
618-
match self {
619-
Ok(_) => false,
620-
Err(e) => f == e,
621-
}
622-
}
623-
624566
/////////////////////////////////////////////////////////////////////////
625567
// Adapter for each variant
626568
/////////////////////////////////////////////////////////////////////////
@@ -1491,6 +1433,68 @@ impl<T, E> Result<T, E> {
14911433
Err(e) => e,
14921434
}
14931435
}
1436+
1437+
/////////////////////////////////////////////////////////////////////////
1438+
// Misc or niche
1439+
/////////////////////////////////////////////////////////////////////////
1440+
1441+
/// Returns `true` if the result is an [`Ok`] value containing the given value.
1442+
///
1443+
/// # Examples
1444+
///
1445+
/// ```
1446+
/// #![feature(option_result_contains)]
1447+
///
1448+
/// let x: Result<u32, &str> = Ok(2);
1449+
/// assert_eq!(x.contains(&2), true);
1450+
///
1451+
/// let x: Result<u32, &str> = Ok(3);
1452+
/// assert_eq!(x.contains(&2), false);
1453+
///
1454+
/// let x: Result<u32, &str> = Err("Some error message");
1455+
/// assert_eq!(x.contains(&2), false);
1456+
/// ```
1457+
#[must_use]
1458+
#[inline]
1459+
#[unstable(feature = "option_result_contains", issue = "62358")]
1460+
pub fn contains<U>(&self, x: &U) -> bool
1461+
where
1462+
U: PartialEq<T>,
1463+
{
1464+
match self {
1465+
Ok(y) => x == y,
1466+
Err(_) => false,
1467+
}
1468+
}
1469+
1470+
/// Returns `true` if the result is an [`Err`] value containing the given value.
1471+
///
1472+
/// # Examples
1473+
///
1474+
/// ```
1475+
/// #![feature(result_contains_err)]
1476+
///
1477+
/// let x: Result<u32, &str> = Ok(2);
1478+
/// assert_eq!(x.contains_err(&"Some error message"), false);
1479+
///
1480+
/// let x: Result<u32, &str> = Err("Some error message");
1481+
/// assert_eq!(x.contains_err(&"Some error message"), true);
1482+
///
1483+
/// let x: Result<u32, &str> = Err("Some other error message");
1484+
/// assert_eq!(x.contains_err(&"Some error message"), false);
1485+
/// ```
1486+
#[must_use]
1487+
#[inline]
1488+
#[unstable(feature = "result_contains_err", issue = "62358")]
1489+
pub fn contains_err<F>(&self, f: &F) -> bool
1490+
where
1491+
F: PartialEq<E>,
1492+
{
1493+
match self {
1494+
Ok(_) => false,
1495+
Err(e) => f == e,
1496+
}
1497+
}
14941498
}
14951499

14961500
impl<T, E> Result<&T, E> {

0 commit comments

Comments
 (0)