Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmp doc examples improvements #110448

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 46 additions & 45 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,11 @@ pub struct AssertParamIsEq<T: Eq + ?Sized> {
/// ```
/// use std::cmp::Ordering;
///
/// let result = 1.cmp(&2);
/// assert_eq!(Ordering::Less, result);
/// assert_eq!(1.cmp(&2), Ordering::Less);
///
/// let result = 1.cmp(&1);
/// assert_eq!(Ordering::Equal, result);
/// assert_eq!(1.cmp(&1), Ordering::Equal);
///
/// let result = 2.cmp(&1);
/// assert_eq!(Ordering::Greater, result);
/// assert_eq!(2.cmp(&1), Ordering::Greater);
/// ```
#[derive(Clone, Copy, Eq, Debug, Hash)]
#[derive_const(PartialOrd, Ord, PartialEq)]
Expand Down Expand Up @@ -790,8 +787,8 @@ pub trait Ord: Eq + PartialOrd<Self> {
/// # Examples
///
/// ```
/// assert_eq!(2, 1.max(2));
/// assert_eq!(2, 2.max(2));
/// assert_eq!(1.max(2), 2);
/// assert_eq!(2.max(2), 2);
/// ```
#[stable(feature = "ord_max_min", since = "1.21.0")]
#[inline]
Expand All @@ -811,8 +808,8 @@ pub trait Ord: Eq + PartialOrd<Self> {
/// # Examples
///
/// ```
/// assert_eq!(1, 1.min(2));
/// assert_eq!(2, 2.min(2));
/// assert_eq!(1.min(2), 1);
/// assert_eq!(2.min(2), 2);
/// ```
#[stable(feature = "ord_max_min", since = "1.21.0")]
#[inline]
Expand All @@ -837,9 +834,9 @@ pub trait Ord: Eq + PartialOrd<Self> {
/// # Examples
///
/// ```
/// assert!((-3).clamp(-2, 1) == -2);
/// assert!(0.clamp(-2, 1) == 0);
/// assert!(2.clamp(-2, 1) == 1);
/// assert_eq!((-3).clamp(-2, 1), -2);
/// assert_eq!(0.clamp(-2, 1), 0);
/// assert_eq!(2.clamp(-2, 1), 1);
/// ```
#[must_use]
#[stable(feature = "clamp", since = "1.50.0")]
Expand Down Expand Up @@ -1070,11 +1067,9 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
/// # Examples
///
/// ```
/// let result = 1.0 < 2.0;
/// assert_eq!(result, true);
///
/// let result = 2.0 < 1.0;
/// assert_eq!(result, false);
/// assert_eq!(1.0 < 1.0, false);
/// assert_eq!(1.0 < 2.0, true);
/// assert_eq!(2.0 < 1.0, false);
/// ```
#[inline]
#[must_use]
Expand All @@ -1089,11 +1084,9 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
/// # Examples
///
/// ```
/// let result = 1.0 <= 2.0;
/// assert_eq!(result, true);
///
/// let result = 2.0 <= 2.0;
/// assert_eq!(result, true);
/// assert_eq!(1.0 <= 1.0, true);
/// assert_eq!(1.0 <= 2.0, true);
/// assert_eq!(2.0 <= 1.0, false);
/// ```
#[inline]
#[must_use]
Expand All @@ -1107,11 +1100,9 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
/// # Examples
///
/// ```
/// let result = 1.0 > 2.0;
/// assert_eq!(result, false);
///
/// let result = 2.0 > 2.0;
/// assert_eq!(result, false);
/// assert_eq!(1.0 > 1.0, false);
/// assert_eq!(1.0 > 2.0, false);
/// assert_eq!(2.0 > 1.0, true);
/// ```
#[inline]
#[must_use]
Expand All @@ -1126,11 +1117,9 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
/// # Examples
///
/// ```
/// let result = 2.0 >= 1.0;
/// assert_eq!(result, true);
///
/// let result = 2.0 >= 2.0;
/// assert_eq!(result, true);
/// assert_eq!(1.0 >= 1.0, true);
/// assert_eq!(1.0 >= 2.0, false);
/// assert_eq!(2.0 >= 1.0, true);
/// ```
#[inline]
#[must_use]
Expand Down Expand Up @@ -1160,8 +1149,8 @@ pub macro PartialOrd($item:item) {
/// ```
/// use std::cmp;
///
/// assert_eq!(1, cmp::min(1, 2));
/// assert_eq!(2, cmp::min(2, 2));
/// assert_eq!(cmp::min(1, 2), 1);
/// assert_eq!(cmp::min(2, 2), 2);
/// ```
#[inline]
#[must_use]
Expand All @@ -1181,8 +1170,11 @@ pub const fn min<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
/// ```
/// use std::cmp;
///
/// assert_eq!(cmp::min_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 1);
/// assert_eq!(cmp::min_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
/// let result = cmp::min_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
/// assert_eq!(result, 1);
///
/// let result = cmp::min_by(-2, 3, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
/// assert_eq!(result, -2);
/// ```
#[inline]
#[must_use]
Expand All @@ -1208,8 +1200,11 @@ where
/// ```
/// use std::cmp;
///
/// assert_eq!(cmp::min_by_key(-2, 1, |x: &i32| x.abs()), 1);
/// assert_eq!(cmp::min_by_key(-2, 2, |x: &i32| x.abs()), -2);
/// let result = cmp::min_by_key(-2, 1, |x: &i32| x.abs());
/// assert_eq!(result, 1);
///
/// let result = cmp::min_by_key(-2, 2, |x: &i32| x.abs());
/// assert_eq!(result, -2);
/// ```
#[inline]
#[must_use]
Expand All @@ -1235,8 +1230,8 @@ where
/// ```
/// use std::cmp;
///
/// assert_eq!(2, cmp::max(1, 2));
/// assert_eq!(2, cmp::max(2, 2));
/// assert_eq!(cmp::max(1, 2), 2);
/// assert_eq!(cmp::max(2, 2), 2);
/// ```
#[inline]
#[must_use]
Expand All @@ -1256,8 +1251,11 @@ pub const fn max<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
/// ```
/// use std::cmp;
///
/// assert_eq!(cmp::max_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
/// assert_eq!(cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 2);
/// let result = cmp::max_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
/// assert_eq!(result, -2);
///
/// let result = cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())) ;
/// assert_eq!(result, 2);
/// ```
#[inline]
#[must_use]
Expand All @@ -1283,8 +1281,11 @@ where
/// ```
/// use std::cmp;
///
/// assert_eq!(cmp::max_by_key(-2, 1, |x: &i32| x.abs()), -2);
/// assert_eq!(cmp::max_by_key(-2, 2, |x: &i32| x.abs()), 2);
/// let result = cmp::max_by_key(-2, 1, |x: &i32| x.abs());
/// assert_eq!(result, -2);
///
/// let result = cmp::max_by_key(-2, 2, |x: &i32| x.abs());
/// assert_eq!(result, 2);
/// ```
#[inline]
#[must_use]
Expand Down