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

Add missing examples on core traits' method #76568

Merged
merged 1 commit into from
Sep 10, 2020
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
77 changes: 77 additions & 0 deletions library/core/src/ops/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ pub trait Add<Rhs = Self> {
type Output;

/// Performs the `+` operation.
///
/// # Example
///
/// ```
/// assert_eq!(12 + 1, 13);
/// ```
Copy link
Contributor

@pickfire pickfire Sep 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mention somewhere what happens when add overflow on debug build and release build? IIRC it behaves differently.

#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn add(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -178,6 +184,12 @@ pub trait Sub<Rhs = Self> {
type Output;

/// Performs the `-` operation.
///
/// # Example
///
/// ```
/// assert_eq!(12 - 1, 11);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn sub(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -300,6 +312,12 @@ pub trait Mul<Rhs = Self> {
type Output;

/// Performs the `*` operation.
///
/// # Example
///
/// ```
/// assert_eq!(12 * 2, 24);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn mul(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -426,6 +444,12 @@ pub trait Div<Rhs = Self> {
type Output;

/// Performs the `/` operation.
///
/// # Example
///
/// ```
/// assert_eq!(12 / 2, 6);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about 12 / 0?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, I think it would be good to have another example to show that it will fail to compile.

/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn div(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -513,6 +537,12 @@ pub trait Rem<Rhs = Self> {
type Output;

/// Performs the `%` operation.
///
/// # Example
///
/// ```
/// assert_eq!(12 % 10, 2);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn rem(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -612,6 +642,13 @@ pub trait Neg {
type Output;

/// Performs the unary `-` operation.
///
/// # Example
///
/// ```
/// let x: i32 = 12;
/// assert_eq!(-x, -12);
Comment on lines +649 to +650
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say something about assert_eq!(-12, -12) and then I realized that was silly lol. So this is fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about assert_eq!(0, -0)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These suggestions are about the meaning of the operations, you're showing off the edge cases. I don't think this is a good place to document edge cases, this is just showing which operations the trait corresponds to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In particular, all these suggestions only make sense for numbers and would be completely out of place if it used Duration or something instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, edge cases are considered part of the meanings too, of course users wouldn't want surprises. Or maybe do you have a better idea where to put these edge cases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd document them on the implementations, not the traits themselves. Take a look at library/core/src/num, maybe. But they certainly don't belong here - this is for every type that implements the trait, not just numbers.

/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn neg(self) -> Self::Output;
Expand Down Expand Up @@ -673,6 +710,14 @@ neg_impl! { isize i8 i16 i32 i64 i128 f32 f64 }
#[doc(alias = "+=")]
pub trait AddAssign<Rhs = Self> {
/// Performs the `+=` operation.
///
/// # Example
///
/// ```
/// let mut x: u32 = 12;
/// x += 1;
/// assert_eq!(x, 13);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn add_assign(&mut self, rhs: Rhs);
}
Expand Down Expand Up @@ -731,6 +776,14 @@ add_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
#[doc(alias = "-=")]
pub trait SubAssign<Rhs = Self> {
/// Performs the `-=` operation.
///
/// # Example
///
/// ```
/// let mut x: u32 = 12;
/// x -= 1;
/// assert_eq!(x, 11);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn sub_assign(&mut self, rhs: Rhs);
}
Expand Down Expand Up @@ -780,6 +833,14 @@ sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
#[doc(alias = "*=")]
pub trait MulAssign<Rhs = Self> {
/// Performs the `*=` operation.
///
/// # Example
///
/// ```
/// let mut x: u32 = 12;
/// x *= 2;
/// assert_eq!(x, 24);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn mul_assign(&mut self, rhs: Rhs);
}
Expand Down Expand Up @@ -829,6 +890,14 @@ mul_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
#[doc(alias = "/=")]
pub trait DivAssign<Rhs = Self> {
/// Performs the `/=` operation.
///
/// # Example
///
/// ```
/// let mut x: u32 = 12;
/// x /= 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about / 0?

/// assert_eq!(x, 6);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn div_assign(&mut self, rhs: Rhs);
}
Expand Down Expand Up @@ -881,6 +950,14 @@ div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
#[doc(alias = "%=")]
pub trait RemAssign<Rhs = Self> {
/// Performs the `%=` operation.
///
/// # Example
///
/// ```
/// let mut x: u32 = 12;
/// x %= 10;
/// assert_eq!(x, 2);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn rem_assign(&mut self, rhs: Rhs);
}
Expand Down