1 /// The addition operator `+`.
3 /// Note that `Rhs` is `Self` by default, but this is not mandatory. For
4 /// example, [`std::time::SystemTime`] implements `Add<Duration>`, which permits
5 /// operations of the form `SystemTime = SystemTime + Duration`.
7 /// [`std::time::SystemTime`]: ../../std/time/struct.SystemTime.html
11 /// ## `Add`able points
14 /// use std::ops::Add;
16 /// #[derive(Debug, Copy, Clone, PartialEq)]
22 /// impl Add for Point {
23 /// type Output = Self;
25 /// fn add(self, other: Self) -> Self {
27 /// x: self.x + other.x,
28 /// y: self.y + other.y,
33 /// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
34 /// Point { x: 3, y: 3 });
37 /// ## Implementing `Add` with generics
39 /// Here is an example of the same `Point` struct implementing the `Add` trait
43 /// use std::ops::Add;
45 /// #[derive(Debug, Copy, Clone, PartialEq)]
51 /// // Notice that the implementation uses the associated type `Output`.
52 /// impl<T: Add<Output = T>> Add for Point<T> {
53 /// type Output = Self;
55 /// fn add(self, other: Self) -> Self::Output {
57 /// x: self.x + other.x,
58 /// y: self.y + other.y,
63 /// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
64 /// Point { x: 3, y: 3 });
67 #[stable(feature = "rust1", since = "1.0.0")]
70 rustc_on_unimplemented(
72 all(_Self = "{integer}", Rhs = "{float}"),
73 message = "cannot add a float to an integer",
76 all(_Self = "{float}", Rhs = "{integer}"),
77 message = "cannot add an integer to a float",
79 message = "cannot add `{Rhs}` to `{Self}`",
80 label = "no implementation for `{Self} + {Rhs}`"
85 rustc_on_unimplemented(
87 all(_Self = "{integer}", Rhs = "{float}"),
88 message = "cannot add a float to an integer",
91 all(_Self = "{float}", Rhs = "{integer}"),
92 message = "cannot add an integer to a float",
94 message = "cannot add `{Rhs}` to `{Self}`",
95 label = "no implementation for `{Self} + {Rhs}`",
100 pub trait Add<Rhs = Self> {
101 /// The resulting type after applying the `+` operator.
102 #[stable(feature = "rust1", since = "1.0.0")]
105 /// Performs the `+` operation.
110 /// assert_eq!(12 + 1, 13);
113 #[stable(feature = "rust1", since = "1.0.0")]
114 fn add(self, rhs: Rhs) -> Self::Output;
117 macro_rules! add_impl {
119 #[stable(feature = "rust1", since = "1.0.0")]
120 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
121 impl const Add for $t {
125 #[rustc_inherit_overflow_checks]
126 fn add(self, other: $t) -> $t { self + other }
129 forward_ref_binop! { impl const Add, add for $t, $t }
133 add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
135 /// The subtraction operator `-`.
137 /// Note that `Rhs` is `Self` by default, but this is not mandatory. For
138 /// example, [`std::time::SystemTime`] implements `Sub<Duration>`, which permits
139 /// operations of the form `SystemTime = SystemTime - Duration`.
141 /// [`std::time::SystemTime`]: ../../std/time/struct.SystemTime.html
145 /// ## `Sub`tractable points
148 /// use std::ops::Sub;
150 /// #[derive(Debug, Copy, Clone, PartialEq)]
156 /// impl Sub for Point {
157 /// type Output = Self;
159 /// fn sub(self, other: Self) -> Self::Output {
161 /// x: self.x - other.x,
162 /// y: self.y - other.y,
167 /// assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
168 /// Point { x: 1, y: 0 });
171 /// ## Implementing `Sub` with generics
173 /// Here is an example of the same `Point` struct implementing the `Sub` trait
177 /// use std::ops::Sub;
179 /// #[derive(Debug, PartialEq)]
180 /// struct Point<T> {
185 /// // Notice that the implementation uses the associated type `Output`.
186 /// impl<T: Sub<Output = T>> Sub for Point<T> {
187 /// type Output = Self;
189 /// fn sub(self, other: Self) -> Self::Output {
191 /// x: self.x - other.x,
192 /// y: self.y - other.y,
197 /// assert_eq!(Point { x: 2, y: 3 } - Point { x: 1, y: 0 },
198 /// Point { x: 1, y: 3 });
201 #[stable(feature = "rust1", since = "1.0.0")]
202 #[rustc_on_unimplemented(
203 message = "cannot subtract `{Rhs}` from `{Self}`",
204 label = "no implementation for `{Self} - {Rhs}`"
207 pub trait Sub<Rhs = Self> {
208 /// The resulting type after applying the `-` operator.
209 #[stable(feature = "rust1", since = "1.0.0")]
212 /// Performs the `-` operation.
217 /// assert_eq!(12 - 1, 11);
220 #[stable(feature = "rust1", since = "1.0.0")]
221 fn sub(self, rhs: Rhs) -> Self::Output;
224 macro_rules! sub_impl {
226 #[stable(feature = "rust1", since = "1.0.0")]
227 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
228 impl const Sub for $t {
232 #[rustc_inherit_overflow_checks]
233 fn sub(self, other: $t) -> $t { self - other }
236 forward_ref_binop! { impl const Sub, sub for $t, $t }
240 sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
242 /// The multiplication operator `*`.
244 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
248 /// ## `Mul`tipliable rational numbers
251 /// use std::ops::Mul;
253 /// // By the fundamental theorem of arithmetic, rational numbers in lowest
254 /// // terms are unique. So, by keeping `Rational`s in reduced form, we can
255 /// // derive `Eq` and `PartialEq`.
256 /// #[derive(Debug, Eq, PartialEq)]
257 /// struct Rational {
258 /// numerator: usize,
259 /// denominator: usize,
263 /// fn new(numerator: usize, denominator: usize) -> Self {
264 /// if denominator == 0 {
265 /// panic!("Zero is an invalid denominator!");
268 /// // Reduce to lowest terms by dividing by the greatest common
270 /// let gcd = gcd(numerator, denominator);
272 /// numerator: numerator / gcd,
273 /// denominator: denominator / gcd,
278 /// impl Mul for Rational {
279 /// // The multiplication of rational numbers is a closed operation.
280 /// type Output = Self;
282 /// fn mul(self, rhs: Self) -> Self {
283 /// let numerator = self.numerator * rhs.numerator;
284 /// let denominator = self.denominator * rhs.denominator;
285 /// Self::new(numerator, denominator)
289 /// // Euclid's two-thousand-year-old algorithm for finding the greatest common
291 /// fn gcd(x: usize, y: usize) -> usize {
302 /// assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
303 /// assert_eq!(Rational::new(2, 3) * Rational::new(3, 4),
304 /// Rational::new(1, 2));
307 /// ## Multiplying vectors by scalars as in linear algebra
310 /// use std::ops::Mul;
312 /// struct Scalar { value: usize }
314 /// #[derive(Debug, PartialEq)]
315 /// struct Vector { value: Vec<usize> }
317 /// impl Mul<Scalar> for Vector {
318 /// type Output = Self;
320 /// fn mul(self, rhs: Scalar) -> Self::Output {
321 /// Self { value: self.value.iter().map(|v| v * rhs.value).collect() }
325 /// let vector = Vector { value: vec![2, 4, 6] };
326 /// let scalar = Scalar { value: 3 };
327 /// assert_eq!(vector * scalar, Vector { value: vec![6, 12, 18] });
330 #[stable(feature = "rust1", since = "1.0.0")]
331 #[rustc_on_unimplemented(
332 message = "cannot multiply `{Self}` by `{Rhs}`",
333 label = "no implementation for `{Self} * {Rhs}`"
336 pub trait Mul<Rhs = Self> {
337 /// The resulting type after applying the `*` operator.
338 #[stable(feature = "rust1", since = "1.0.0")]
341 /// Performs the `*` operation.
346 /// assert_eq!(12 * 2, 24);
349 #[stable(feature = "rust1", since = "1.0.0")]
350 fn mul(self, rhs: Rhs) -> Self::Output;
353 macro_rules! mul_impl {
355 #[stable(feature = "rust1", since = "1.0.0")]
356 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
357 impl const Mul for $t {
361 #[rustc_inherit_overflow_checks]
362 fn mul(self, other: $t) -> $t { self * other }
365 forward_ref_binop! { impl const Mul, mul for $t, $t }
369 mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
371 /// The division operator `/`.
373 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
377 /// ## `Div`idable rational numbers
380 /// use std::ops::Div;
382 /// // By the fundamental theorem of arithmetic, rational numbers in lowest
383 /// // terms are unique. So, by keeping `Rational`s in reduced form, we can
384 /// // derive `Eq` and `PartialEq`.
385 /// #[derive(Debug, Eq, PartialEq)]
386 /// struct Rational {
387 /// numerator: usize,
388 /// denominator: usize,
392 /// fn new(numerator: usize, denominator: usize) -> Self {
393 /// if denominator == 0 {
394 /// panic!("Zero is an invalid denominator!");
397 /// // Reduce to lowest terms by dividing by the greatest common
399 /// let gcd = gcd(numerator, denominator);
401 /// numerator: numerator / gcd,
402 /// denominator: denominator / gcd,
407 /// impl Div for Rational {
408 /// // The division of rational numbers is a closed operation.
409 /// type Output = Self;
411 /// fn div(self, rhs: Self) -> Self::Output {
412 /// if rhs.numerator == 0 {
413 /// panic!("Cannot divide by zero-valued `Rational`!");
416 /// let numerator = self.numerator * rhs.denominator;
417 /// let denominator = self.denominator * rhs.numerator;
418 /// Self::new(numerator, denominator)
422 /// // Euclid's two-thousand-year-old algorithm for finding the greatest common
424 /// fn gcd(x: usize, y: usize) -> usize {
435 /// assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
436 /// assert_eq!(Rational::new(1, 2) / Rational::new(3, 4),
437 /// Rational::new(2, 3));
440 /// ## Dividing vectors by scalars as in linear algebra
443 /// use std::ops::Div;
445 /// struct Scalar { value: f32 }
447 /// #[derive(Debug, PartialEq)]
448 /// struct Vector { value: Vec<f32> }
450 /// impl Div<Scalar> for Vector {
451 /// type Output = Self;
453 /// fn div(self, rhs: Scalar) -> Self::Output {
454 /// Self { value: self.value.iter().map(|v| v / rhs.value).collect() }
458 /// let scalar = Scalar { value: 2f32 };
459 /// let vector = Vector { value: vec![2f32, 4f32, 6f32] };
460 /// assert_eq!(vector / scalar, Vector { value: vec![1f32, 2f32, 3f32] });
463 #[stable(feature = "rust1", since = "1.0.0")]
464 #[rustc_on_unimplemented(
465 message = "cannot divide `{Self}` by `{Rhs}`",
466 label = "no implementation for `{Self} / {Rhs}`"
469 pub trait Div<Rhs = Self> {
470 /// The resulting type after applying the `/` operator.
471 #[stable(feature = "rust1", since = "1.0.0")]
474 /// Performs the `/` operation.
479 /// assert_eq!(12 / 2, 6);
482 #[stable(feature = "rust1", since = "1.0.0")]
483 fn div(self, rhs: Rhs) -> Self::Output;
486 macro_rules! div_impl_integer {
487 ($(($($t:ty)*) => $panic:expr),*) => ($($(
488 /// This operation rounds towards zero, truncating any
489 /// fractional part of the exact result.
494 #[stable(feature = "rust1", since = "1.0.0")]
495 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
496 impl const Div for $t {
500 fn div(self, other: $t) -> $t { self / other }
503 forward_ref_binop! { impl const Div, div for $t, $t }
508 (usize u8 u16 u32 u64 u128) => "This operation will panic if `other == 0`.",
509 (isize i8 i16 i32 i64 i128) => "This operation will panic if `other == 0` or the division results in overflow."
512 macro_rules! div_impl_float {
514 #[stable(feature = "rust1", since = "1.0.0")]
515 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
516 impl const Div for $t {
520 fn div(self, other: $t) -> $t { self / other }
523 forward_ref_binop! { impl const Div, div for $t, $t }
527 div_impl_float! { f32 f64 }
529 /// The remainder operator `%`.
531 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
535 /// This example implements `Rem` on a `SplitSlice` object. After `Rem` is
536 /// implemented, one can use the `%` operator to find out what the remaining
537 /// elements of the slice would be after splitting it into equal slices of a
541 /// use std::ops::Rem;
543 /// #[derive(PartialEq, Debug)]
544 /// struct SplitSlice<'a, T: 'a> {
548 /// impl<'a, T> Rem<usize> for SplitSlice<'a, T> {
549 /// type Output = Self;
551 /// fn rem(self, modulus: usize) -> Self::Output {
552 /// let len = self.slice.len();
553 /// let rem = len % modulus;
554 /// let start = len - rem;
555 /// Self {slice: &self.slice[start..]}
559 /// // If we were to divide &[0, 1, 2, 3, 4, 5, 6, 7] into slices of size 3,
560 /// // the remainder would be &[6, 7].
561 /// assert_eq!(SplitSlice { slice: &[0, 1, 2, 3, 4, 5, 6, 7] } % 3,
562 /// SplitSlice { slice: &[6, 7] });
565 #[stable(feature = "rust1", since = "1.0.0")]
566 #[rustc_on_unimplemented(
567 message = "cannot mod `{Self}` by `{Rhs}`",
568 label = "no implementation for `{Self} % {Rhs}`"
571 pub trait Rem<Rhs = Self> {
572 /// The resulting type after applying the `%` operator.
573 #[stable(feature = "rust1", since = "1.0.0")]
576 /// Performs the `%` operation.
581 /// assert_eq!(12 % 10, 2);
584 #[stable(feature = "rust1", since = "1.0.0")]
585 fn rem(self, rhs: Rhs) -> Self::Output;
588 macro_rules! rem_impl_integer {
589 ($(($($t:ty)*) => $panic:expr),*) => ($($(
590 /// This operation satisfies `n % d == n - (n / d) * d`. The
591 /// result has the same sign as the left operand.
596 #[stable(feature = "rust1", since = "1.0.0")]
597 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
598 impl const Rem for $t {
602 fn rem(self, other: $t) -> $t { self % other }
605 forward_ref_binop! { impl const Rem, rem for $t, $t }
610 (usize u8 u16 u32 u64 u128) => "This operation will panic if `other == 0`.",
611 (isize i8 i16 i32 i64 i128) => "This operation will panic if `other == 0` or if `self / other` results in overflow."
614 macro_rules! rem_impl_float {
617 /// The remainder from the division of two floats.
619 /// The remainder has the same sign as the dividend and is computed as:
620 /// `x - (x / y).trunc() * y`.
624 /// let x: f32 = 50.50;
625 /// let y: f32 = 8.125;
626 /// let remainder = x - (x / y).trunc() * y;
628 /// // The answer to both operations is 1.75
629 /// assert_eq!(x % y, remainder);
631 #[stable(feature = "rust1", since = "1.0.0")]
632 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
633 impl const Rem for $t {
637 fn rem(self, other: $t) -> $t { self % other }
640 forward_ref_binop! { impl const Rem, rem for $t, $t }
644 rem_impl_float! { f32 f64 }
646 /// The unary negation operator `-`.
650 /// An implementation of `Neg` for `Sign`, which allows the use of `-` to
651 /// negate its value.
654 /// use std::ops::Neg;
656 /// #[derive(Debug, PartialEq)]
663 /// impl Neg for Sign {
664 /// type Output = Self;
666 /// fn neg(self) -> Self::Output {
668 /// Sign::Negative => Sign::Positive,
669 /// Sign::Zero => Sign::Zero,
670 /// Sign::Positive => Sign::Negative,
675 /// // A negative positive is a negative.
676 /// assert_eq!(-Sign::Positive, Sign::Negative);
677 /// // A double negative is a positive.
678 /// assert_eq!(-Sign::Negative, Sign::Positive);
679 /// // Zero is its own negation.
680 /// assert_eq!(-Sign::Zero, Sign::Zero);
683 #[stable(feature = "rust1", since = "1.0.0")]
686 /// The resulting type after applying the `-` operator.
687 #[stable(feature = "rust1", since = "1.0.0")]
690 /// Performs the unary `-` operation.
696 /// assert_eq!(-x, -12);
699 #[stable(feature = "rust1", since = "1.0.0")]
700 fn neg(self) -> Self::Output;
703 macro_rules! neg_impl {
705 #[stable(feature = "rust1", since = "1.0.0")]
706 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
707 impl const Neg for $t {
711 #[rustc_inherit_overflow_checks]
712 fn neg(self) -> $t { -self }
715 forward_ref_unop! { impl const Neg, neg for $t }
719 neg_impl! { isize i8 i16 i32 i64 i128 f32 f64 }
721 /// The addition assignment operator `+=`.
725 /// This example creates a `Point` struct that implements the `AddAssign`
726 /// trait, and then demonstrates add-assigning to a mutable `Point`.
729 /// use std::ops::AddAssign;
731 /// #[derive(Debug, Copy, Clone, PartialEq)]
737 /// impl AddAssign for Point {
738 /// fn add_assign(&mut self, other: Self) {
740 /// x: self.x + other.x,
741 /// y: self.y + other.y,
746 /// let mut point = Point { x: 1, y: 0 };
747 /// point += Point { x: 2, y: 3 };
748 /// assert_eq!(point, Point { x: 3, y: 3 });
750 #[lang = "add_assign"]
751 #[stable(feature = "op_assign_traits", since = "1.8.0")]
752 #[rustc_on_unimplemented(
753 message = "cannot add-assign `{Rhs}` to `{Self}`",
754 label = "no implementation for `{Self} += {Rhs}`"
758 pub trait AddAssign<Rhs = Self> {
759 /// Performs the `+=` operation.
764 /// let mut x: u32 = 12;
766 /// assert_eq!(x, 13);
768 #[stable(feature = "op_assign_traits", since = "1.8.0")]
769 fn add_assign(&mut self, rhs: Rhs);
772 macro_rules! add_assign_impl {
774 #[stable(feature = "op_assign_traits", since = "1.8.0")]
775 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
776 impl const AddAssign for $t {
778 #[rustc_inherit_overflow_checks]
779 fn add_assign(&mut self, other: $t) { *self += other }
782 forward_ref_op_assign! { impl const AddAssign, add_assign for $t, $t }
786 add_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
788 /// The subtraction assignment operator `-=`.
792 /// This example creates a `Point` struct that implements the `SubAssign`
793 /// trait, and then demonstrates sub-assigning to a mutable `Point`.
796 /// use std::ops::SubAssign;
798 /// #[derive(Debug, Copy, Clone, PartialEq)]
804 /// impl SubAssign for Point {
805 /// fn sub_assign(&mut self, other: Self) {
807 /// x: self.x - other.x,
808 /// y: self.y - other.y,
813 /// let mut point = Point { x: 3, y: 3 };
814 /// point -= Point { x: 2, y: 3 };
815 /// assert_eq!(point, Point {x: 1, y: 0});
817 #[lang = "sub_assign"]
818 #[stable(feature = "op_assign_traits", since = "1.8.0")]
819 #[rustc_on_unimplemented(
820 message = "cannot subtract-assign `{Rhs}` from `{Self}`",
821 label = "no implementation for `{Self} -= {Rhs}`"
825 pub trait SubAssign<Rhs = Self> {
826 /// Performs the `-=` operation.
831 /// let mut x: u32 = 12;
833 /// assert_eq!(x, 11);
835 #[stable(feature = "op_assign_traits", since = "1.8.0")]
836 fn sub_assign(&mut self, rhs: Rhs);
839 macro_rules! sub_assign_impl {
841 #[stable(feature = "op_assign_traits", since = "1.8.0")]
842 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
843 impl const SubAssign for $t {
845 #[rustc_inherit_overflow_checks]
846 fn sub_assign(&mut self, other: $t) { *self -= other }
849 forward_ref_op_assign! { impl const SubAssign, sub_assign for $t, $t }
853 sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
855 /// The multiplication assignment operator `*=`.
860 /// use std::ops::MulAssign;
862 /// #[derive(Debug, PartialEq)]
863 /// struct Frequency { hertz: f64 }
865 /// impl MulAssign<f64> for Frequency {
866 /// fn mul_assign(&mut self, rhs: f64) {
867 /// self.hertz *= rhs;
871 /// let mut frequency = Frequency { hertz: 50.0 };
872 /// frequency *= 4.0;
873 /// assert_eq!(Frequency { hertz: 200.0 }, frequency);
875 #[lang = "mul_assign"]
876 #[stable(feature = "op_assign_traits", since = "1.8.0")]
877 #[rustc_on_unimplemented(
878 message = "cannot multiply-assign `{Self}` by `{Rhs}`",
879 label = "no implementation for `{Self} *= {Rhs}`"
883 pub trait MulAssign<Rhs = Self> {
884 /// Performs the `*=` operation.
889 /// let mut x: u32 = 12;
891 /// assert_eq!(x, 24);
893 #[stable(feature = "op_assign_traits", since = "1.8.0")]
894 fn mul_assign(&mut self, rhs: Rhs);
897 macro_rules! mul_assign_impl {
899 #[stable(feature = "op_assign_traits", since = "1.8.0")]
900 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
901 impl const MulAssign for $t {
903 #[rustc_inherit_overflow_checks]
904 fn mul_assign(&mut self, other: $t) { *self *= other }
907 forward_ref_op_assign! { impl const MulAssign, mul_assign for $t, $t }
911 mul_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
913 /// The division assignment operator `/=`.
918 /// use std::ops::DivAssign;
920 /// #[derive(Debug, PartialEq)]
921 /// struct Frequency { hertz: f64 }
923 /// impl DivAssign<f64> for Frequency {
924 /// fn div_assign(&mut self, rhs: f64) {
925 /// self.hertz /= rhs;
929 /// let mut frequency = Frequency { hertz: 200.0 };
930 /// frequency /= 4.0;
931 /// assert_eq!(Frequency { hertz: 50.0 }, frequency);
933 #[lang = "div_assign"]
934 #[stable(feature = "op_assign_traits", since = "1.8.0")]
935 #[rustc_on_unimplemented(
936 message = "cannot divide-assign `{Self}` by `{Rhs}`",
937 label = "no implementation for `{Self} /= {Rhs}`"
941 pub trait DivAssign<Rhs = Self> {
942 /// Performs the `/=` operation.
947 /// let mut x: u32 = 12;
949 /// assert_eq!(x, 6);
951 #[stable(feature = "op_assign_traits", since = "1.8.0")]
952 fn div_assign(&mut self, rhs: Rhs);
955 macro_rules! div_assign_impl {
957 #[stable(feature = "op_assign_traits", since = "1.8.0")]
958 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
959 impl const DivAssign for $t {
961 fn div_assign(&mut self, other: $t) { *self /= other }
964 forward_ref_op_assign! { impl const DivAssign, div_assign for $t, $t }
968 div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
970 /// The remainder assignment operator `%=`.
975 /// use std::ops::RemAssign;
977 /// struct CookieJar { cookies: u32 }
979 /// impl RemAssign<u32> for CookieJar {
980 /// fn rem_assign(&mut self, piles: u32) {
981 /// self.cookies %= piles;
985 /// let mut jar = CookieJar { cookies: 31 };
988 /// println!("Splitting up {} cookies into {} even piles!", jar.cookies, piles);
992 /// println!("{} cookies remain in the cookie jar!", jar.cookies);
994 #[lang = "rem_assign"]
995 #[stable(feature = "op_assign_traits", since = "1.8.0")]
996 #[rustc_on_unimplemented(
997 message = "cannot mod-assign `{Self}` by `{Rhs}``",
998 label = "no implementation for `{Self} %= {Rhs}`"
1001 #[doc(alias = "%=")]
1002 pub trait RemAssign<Rhs = Self> {
1003 /// Performs the `%=` operation.
1008 /// let mut x: u32 = 12;
1010 /// assert_eq!(x, 2);
1012 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1013 fn rem_assign(&mut self, rhs: Rhs);
1016 macro_rules! rem_assign_impl {
1018 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1019 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
1020 impl const RemAssign for $t {
1022 fn rem_assign(&mut self, other: $t) { *self %= other }
1025 forward_ref_op_assign! { impl const RemAssign, rem_assign for $t, $t }
1029 rem_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }