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")]
68 #[rustc_on_unimplemented(
69 on(all(_Self
= "{integer}", Rhs
= "{float}"), message
= "cannot add a float to an integer",),
70 on(all(_Self
= "{float}", Rhs
= "{integer}"), message
= "cannot add an integer to a float",),
71 message
= "cannot add `{Rhs}` to `{Self}`",
72 label
= "no implementation for `{Self} + {Rhs}`",
77 pub trait Add
<Rhs
= Self> {
78 /// The resulting type after applying the `+` operator.
79 #[stable(feature = "rust1", since = "1.0.0")]
82 /// Performs the `+` operation.
87 /// assert_eq!(12 + 1, 13);
90 #[stable(feature = "rust1", since = "1.0.0")]
91 fn add(self, rhs
: Rhs
) -> Self::Output
;
94 macro_rules
! add_impl
{
96 #[stable(feature = "rust1", since = "1.0.0")]
97 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
98 impl const Add
for $t
{
102 #[rustc_inherit_overflow_checks]
103 fn add(self, other
: $t
) -> $t { self + other }
106 forward_ref_binop
! { impl const Add, add for $t, $t }
110 add_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
112 /// The subtraction operator `-`.
114 /// Note that `Rhs` is `Self` by default, but this is not mandatory. For
115 /// example, [`std::time::SystemTime`] implements `Sub<Duration>`, which permits
116 /// operations of the form `SystemTime = SystemTime - Duration`.
118 /// [`std::time::SystemTime`]: ../../std/time/struct.SystemTime.html
122 /// ## `Sub`tractable points
125 /// use std::ops::Sub;
127 /// #[derive(Debug, Copy, Clone, PartialEq)]
133 /// impl Sub for Point {
134 /// type Output = Self;
136 /// fn sub(self, other: Self) -> Self::Output {
138 /// x: self.x - other.x,
139 /// y: self.y - other.y,
144 /// assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
145 /// Point { x: 1, y: 0 });
148 /// ## Implementing `Sub` with generics
150 /// Here is an example of the same `Point` struct implementing the `Sub` trait
154 /// use std::ops::Sub;
156 /// #[derive(Debug, PartialEq)]
157 /// struct Point<T> {
162 /// // Notice that the implementation uses the associated type `Output`.
163 /// impl<T: Sub<Output = T>> Sub for Point<T> {
164 /// type Output = Self;
166 /// fn sub(self, other: Self) -> Self::Output {
168 /// x: self.x - other.x,
169 /// y: self.y - other.y,
174 /// assert_eq!(Point { x: 2, y: 3 } - Point { x: 1, y: 0 },
175 /// Point { x: 1, y: 3 });
178 #[stable(feature = "rust1", since = "1.0.0")]
179 #[rustc_on_unimplemented(
180 message
= "cannot subtract `{Rhs}` from `{Self}`",
181 label
= "no implementation for `{Self} - {Rhs}`",
186 pub trait Sub
<Rhs
= Self> {
187 /// The resulting type after applying the `-` operator.
188 #[stable(feature = "rust1", since = "1.0.0")]
191 /// Performs the `-` operation.
196 /// assert_eq!(12 - 1, 11);
199 #[stable(feature = "rust1", since = "1.0.0")]
200 fn sub(self, rhs
: Rhs
) -> Self::Output
;
203 macro_rules
! sub_impl
{
205 #[stable(feature = "rust1", since = "1.0.0")]
206 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
207 impl const Sub
for $t
{
211 #[rustc_inherit_overflow_checks]
212 fn sub(self, other
: $t
) -> $t { self - other }
215 forward_ref_binop
! { impl const Sub, sub for $t, $t }
219 sub_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
221 /// The multiplication operator `*`.
223 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
227 /// ## `Mul`tipliable rational numbers
230 /// use std::ops::Mul;
232 /// // By the fundamental theorem of arithmetic, rational numbers in lowest
233 /// // terms are unique. So, by keeping `Rational`s in reduced form, we can
234 /// // derive `Eq` and `PartialEq`.
235 /// #[derive(Debug, Eq, PartialEq)]
236 /// struct Rational {
237 /// numerator: usize,
238 /// denominator: usize,
242 /// fn new(numerator: usize, denominator: usize) -> Self {
243 /// if denominator == 0 {
244 /// panic!("Zero is an invalid denominator!");
247 /// // Reduce to lowest terms by dividing by the greatest common
249 /// let gcd = gcd(numerator, denominator);
251 /// numerator: numerator / gcd,
252 /// denominator: denominator / gcd,
257 /// impl Mul for Rational {
258 /// // The multiplication of rational numbers is a closed operation.
259 /// type Output = Self;
261 /// fn mul(self, rhs: Self) -> Self {
262 /// let numerator = self.numerator * rhs.numerator;
263 /// let denominator = self.denominator * rhs.denominator;
264 /// Self::new(numerator, denominator)
268 /// // Euclid's two-thousand-year-old algorithm for finding the greatest common
270 /// fn gcd(x: usize, y: usize) -> usize {
281 /// assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
282 /// assert_eq!(Rational::new(2, 3) * Rational::new(3, 4),
283 /// Rational::new(1, 2));
286 /// ## Multiplying vectors by scalars as in linear algebra
289 /// use std::ops::Mul;
291 /// struct Scalar { value: usize }
293 /// #[derive(Debug, PartialEq)]
294 /// struct Vector { value: Vec<usize> }
296 /// impl Mul<Scalar> for Vector {
297 /// type Output = Self;
299 /// fn mul(self, rhs: Scalar) -> Self::Output {
300 /// Self { value: self.value.iter().map(|v| v * rhs.value).collect() }
304 /// let vector = Vector { value: vec![2, 4, 6] };
305 /// let scalar = Scalar { value: 3 };
306 /// assert_eq!(vector * scalar, Vector { value: vec![6, 12, 18] });
309 #[stable(feature = "rust1", since = "1.0.0")]
310 #[rustc_on_unimplemented(
311 message
= "cannot multiply `{Self}` by `{Rhs}`",
312 label
= "no implementation for `{Self} * {Rhs}`"
316 pub trait Mul
<Rhs
= Self> {
317 /// The resulting type after applying the `*` operator.
318 #[stable(feature = "rust1", since = "1.0.0")]
321 /// Performs the `*` operation.
326 /// assert_eq!(12 * 2, 24);
329 #[stable(feature = "rust1", since = "1.0.0")]
330 fn mul(self, rhs
: Rhs
) -> Self::Output
;
333 macro_rules
! mul_impl
{
335 #[stable(feature = "rust1", since = "1.0.0")]
336 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
337 impl const Mul
for $t
{
341 #[rustc_inherit_overflow_checks]
342 fn mul(self, other
: $t
) -> $t { self * other }
345 forward_ref_binop
! { impl const Mul, mul for $t, $t }
349 mul_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
351 /// The division operator `/`.
353 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
357 /// ## `Div`idable rational numbers
360 /// use std::ops::Div;
362 /// // By the fundamental theorem of arithmetic, rational numbers in lowest
363 /// // terms are unique. So, by keeping `Rational`s in reduced form, we can
364 /// // derive `Eq` and `PartialEq`.
365 /// #[derive(Debug, Eq, PartialEq)]
366 /// struct Rational {
367 /// numerator: usize,
368 /// denominator: usize,
372 /// fn new(numerator: usize, denominator: usize) -> Self {
373 /// if denominator == 0 {
374 /// panic!("Zero is an invalid denominator!");
377 /// // Reduce to lowest terms by dividing by the greatest common
379 /// let gcd = gcd(numerator, denominator);
381 /// numerator: numerator / gcd,
382 /// denominator: denominator / gcd,
387 /// impl Div for Rational {
388 /// // The division of rational numbers is a closed operation.
389 /// type Output = Self;
391 /// fn div(self, rhs: Self) -> Self::Output {
392 /// if rhs.numerator == 0 {
393 /// panic!("Cannot divide by zero-valued `Rational`!");
396 /// let numerator = self.numerator * rhs.denominator;
397 /// let denominator = self.denominator * rhs.numerator;
398 /// Self::new(numerator, denominator)
402 /// // Euclid's two-thousand-year-old algorithm for finding the greatest common
404 /// fn gcd(x: usize, y: usize) -> usize {
415 /// assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
416 /// assert_eq!(Rational::new(1, 2) / Rational::new(3, 4),
417 /// Rational::new(2, 3));
420 /// ## Dividing vectors by scalars as in linear algebra
423 /// use std::ops::Div;
425 /// struct Scalar { value: f32 }
427 /// #[derive(Debug, PartialEq)]
428 /// struct Vector { value: Vec<f32> }
430 /// impl Div<Scalar> for Vector {
431 /// type Output = Self;
433 /// fn div(self, rhs: Scalar) -> Self::Output {
434 /// Self { value: self.value.iter().map(|v| v / rhs.value).collect() }
438 /// let scalar = Scalar { value: 2f32 };
439 /// let vector = Vector { value: vec![2f32, 4f32, 6f32] };
440 /// assert_eq!(vector / scalar, Vector { value: vec![1f32, 2f32, 3f32] });
443 #[stable(feature = "rust1", since = "1.0.0")]
444 #[rustc_on_unimplemented(
445 message
= "cannot divide `{Self}` by `{Rhs}`",
446 label
= "no implementation for `{Self} / {Rhs}`"
450 pub trait Div
<Rhs
= Self> {
451 /// The resulting type after applying the `/` operator.
452 #[stable(feature = "rust1", since = "1.0.0")]
455 /// Performs the `/` operation.
460 /// assert_eq!(12 / 2, 6);
463 #[stable(feature = "rust1", since = "1.0.0")]
464 fn div(self, rhs
: Rhs
) -> Self::Output
;
467 macro_rules
! div_impl_integer
{
468 ($
(($
($t
:ty
)*) => $panic
:expr
),*) => ($
($
(
469 /// This operation rounds towards zero, truncating any
470 /// fractional part of the exact result.
475 #[stable(feature = "rust1", since = "1.0.0")]
476 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
477 impl const Div
for $t
{
481 fn div(self, other
: $t
) -> $t { self / other }
484 forward_ref_binop
! { impl const Div, div for $t, $t }
489 (usize u8 u16 u32 u64 u128
) => "This operation will panic if `other == 0`.",
490 (isize i8 i16 i32 i64 i128
) => "This operation will panic if `other == 0` or the division results in overflow."
493 macro_rules
! div_impl_float
{
495 #[stable(feature = "rust1", since = "1.0.0")]
496 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
497 impl const Div
for $t
{
501 fn div(self, other
: $t
) -> $t { self / other }
504 forward_ref_binop
! { impl const Div, div for $t, $t }
508 div_impl_float
! { f32 f64 }
510 /// The remainder operator `%`.
512 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
516 /// This example implements `Rem` on a `SplitSlice` object. After `Rem` is
517 /// implemented, one can use the `%` operator to find out what the remaining
518 /// elements of the slice would be after splitting it into equal slices of a
522 /// use std::ops::Rem;
524 /// #[derive(PartialEq, Debug)]
525 /// struct SplitSlice<'a, T: 'a> {
529 /// impl<'a, T> Rem<usize> for SplitSlice<'a, T> {
530 /// type Output = Self;
532 /// fn rem(self, modulus: usize) -> Self::Output {
533 /// let len = self.slice.len();
534 /// let rem = len % modulus;
535 /// let start = len - rem;
536 /// Self {slice: &self.slice[start..]}
540 /// // If we were to divide &[0, 1, 2, 3, 4, 5, 6, 7] into slices of size 3,
541 /// // the remainder would be &[6, 7].
542 /// assert_eq!(SplitSlice { slice: &[0, 1, 2, 3, 4, 5, 6, 7] } % 3,
543 /// SplitSlice { slice: &[6, 7] });
546 #[stable(feature = "rust1", since = "1.0.0")]
547 #[rustc_on_unimplemented(
548 message
= "cannot mod `{Self}` by `{Rhs}`",
549 label
= "no implementation for `{Self} % {Rhs}`"
553 pub trait Rem
<Rhs
= Self> {
554 /// The resulting type after applying the `%` operator.
555 #[stable(feature = "rust1", since = "1.0.0")]
558 /// Performs the `%` operation.
563 /// assert_eq!(12 % 10, 2);
566 #[stable(feature = "rust1", since = "1.0.0")]
567 fn rem(self, rhs
: Rhs
) -> Self::Output
;
570 macro_rules
! rem_impl_integer
{
571 ($
(($
($t
:ty
)*) => $panic
:expr
),*) => ($
($
(
572 /// This operation satisfies `n % d == n - (n / d) * d`. The
573 /// result has the same sign as the left operand.
578 #[stable(feature = "rust1", since = "1.0.0")]
579 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
580 impl const Rem
for $t
{
584 fn rem(self, other
: $t
) -> $t { self % other }
587 forward_ref_binop
! { impl const Rem, rem for $t, $t }
592 (usize u8 u16 u32 u64 u128
) => "This operation will panic if `other == 0`.",
593 (isize i8 i16 i32 i64 i128
) => "This operation will panic if `other == 0` or if `self / other` results in overflow."
596 macro_rules
! rem_impl_float
{
599 /// The remainder from the division of two floats.
601 /// The remainder has the same sign as the dividend and is computed as:
602 /// `x - (x / y).trunc() * y`.
606 /// let x: f32 = 50.50;
607 /// let y: f32 = 8.125;
608 /// let remainder = x - (x / y).trunc() * y;
610 /// // The answer to both operations is 1.75
611 /// assert_eq!(x % y, remainder);
613 #[stable(feature = "rust1", since = "1.0.0")]
614 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
615 impl const Rem
for $t
{
619 fn rem(self, other
: $t
) -> $t { self % other }
622 forward_ref_binop
! { impl const Rem, rem for $t, $t }
626 rem_impl_float
! { f32 f64 }
628 /// The unary negation operator `-`.
632 /// An implementation of `Neg` for `Sign`, which allows the use of `-` to
633 /// negate its value.
636 /// use std::ops::Neg;
638 /// #[derive(Debug, PartialEq)]
645 /// impl Neg for Sign {
646 /// type Output = Self;
648 /// fn neg(self) -> Self::Output {
650 /// Sign::Negative => Sign::Positive,
651 /// Sign::Zero => Sign::Zero,
652 /// Sign::Positive => Sign::Negative,
657 /// // A negative positive is a negative.
658 /// assert_eq!(-Sign::Positive, Sign::Negative);
659 /// // A double negative is a positive.
660 /// assert_eq!(-Sign::Negative, Sign::Positive);
661 /// // Zero is its own negation.
662 /// assert_eq!(-Sign::Zero, Sign::Zero);
665 #[stable(feature = "rust1", since = "1.0.0")]
669 /// The resulting type after applying the `-` operator.
670 #[stable(feature = "rust1", since = "1.0.0")]
673 /// Performs the unary `-` operation.
679 /// assert_eq!(-x, -12);
682 #[stable(feature = "rust1", since = "1.0.0")]
683 fn neg(self) -> Self::Output
;
686 macro_rules
! neg_impl
{
688 #[stable(feature = "rust1", since = "1.0.0")]
689 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
690 impl const Neg
for $t
{
694 #[rustc_inherit_overflow_checks]
695 fn neg(self) -> $t { -self }
698 forward_ref_unop
! { impl const Neg, neg for $t }
702 neg_impl
! { isize i8 i16 i32 i64 i128 f32 f64 }
704 /// The addition assignment operator `+=`.
708 /// This example creates a `Point` struct that implements the `AddAssign`
709 /// trait, and then demonstrates add-assigning to a mutable `Point`.
712 /// use std::ops::AddAssign;
714 /// #[derive(Debug, Copy, Clone, PartialEq)]
720 /// impl AddAssign for Point {
721 /// fn add_assign(&mut self, other: Self) {
723 /// x: self.x + other.x,
724 /// y: self.y + other.y,
729 /// let mut point = Point { x: 1, y: 0 };
730 /// point += Point { x: 2, y: 3 };
731 /// assert_eq!(point, Point { x: 3, y: 3 });
733 #[lang = "add_assign"]
734 #[stable(feature = "op_assign_traits", since = "1.8.0")]
735 #[rustc_on_unimplemented(
736 message
= "cannot add-assign `{Rhs}` to `{Self}`",
737 label
= "no implementation for `{Self} += {Rhs}`"
742 pub trait AddAssign
<Rhs
= Self> {
743 /// Performs the `+=` operation.
748 /// let mut x: u32 = 12;
750 /// assert_eq!(x, 13);
752 #[stable(feature = "op_assign_traits", since = "1.8.0")]
753 fn add_assign(&mut self, rhs
: Rhs
);
756 macro_rules
! add_assign_impl
{
758 #[stable(feature = "op_assign_traits", since = "1.8.0")]
759 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
760 impl const AddAssign
for $t
{
762 #[rustc_inherit_overflow_checks]
763 fn add_assign(&mut self, other
: $t
) { *self += other }
766 forward_ref_op_assign
! { impl const AddAssign, add_assign for $t, $t }
770 add_assign_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
772 /// The subtraction assignment operator `-=`.
776 /// This example creates a `Point` struct that implements the `SubAssign`
777 /// trait, and then demonstrates sub-assigning to a mutable `Point`.
780 /// use std::ops::SubAssign;
782 /// #[derive(Debug, Copy, Clone, PartialEq)]
788 /// impl SubAssign for Point {
789 /// fn sub_assign(&mut self, other: Self) {
791 /// x: self.x - other.x,
792 /// y: self.y - other.y,
797 /// let mut point = Point { x: 3, y: 3 };
798 /// point -= Point { x: 2, y: 3 };
799 /// assert_eq!(point, Point {x: 1, y: 0});
801 #[lang = "sub_assign"]
802 #[stable(feature = "op_assign_traits", since = "1.8.0")]
803 #[rustc_on_unimplemented(
804 message
= "cannot subtract-assign `{Rhs}` from `{Self}`",
805 label
= "no implementation for `{Self} -= {Rhs}`"
810 pub trait SubAssign
<Rhs
= Self> {
811 /// Performs the `-=` operation.
816 /// let mut x: u32 = 12;
818 /// assert_eq!(x, 11);
820 #[stable(feature = "op_assign_traits", since = "1.8.0")]
821 fn sub_assign(&mut self, rhs
: Rhs
);
824 macro_rules
! sub_assign_impl
{
826 #[stable(feature = "op_assign_traits", since = "1.8.0")]
827 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
828 impl const SubAssign
for $t
{
830 #[rustc_inherit_overflow_checks]
831 fn sub_assign(&mut self, other
: $t
) { *self -= other }
834 forward_ref_op_assign
! { impl const SubAssign, sub_assign for $t, $t }
838 sub_assign_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
840 /// The multiplication assignment operator `*=`.
845 /// use std::ops::MulAssign;
847 /// #[derive(Debug, PartialEq)]
848 /// struct Frequency { hertz: f64 }
850 /// impl MulAssign<f64> for Frequency {
851 /// fn mul_assign(&mut self, rhs: f64) {
852 /// self.hertz *= rhs;
856 /// let mut frequency = Frequency { hertz: 50.0 };
857 /// frequency *= 4.0;
858 /// assert_eq!(Frequency { hertz: 200.0 }, frequency);
860 #[lang = "mul_assign"]
861 #[stable(feature = "op_assign_traits", since = "1.8.0")]
862 #[rustc_on_unimplemented(
863 message
= "cannot multiply-assign `{Self}` by `{Rhs}`",
864 label
= "no implementation for `{Self} *= {Rhs}`"
869 pub trait MulAssign
<Rhs
= Self> {
870 /// Performs the `*=` operation.
875 /// let mut x: u32 = 12;
877 /// assert_eq!(x, 24);
879 #[stable(feature = "op_assign_traits", since = "1.8.0")]
880 fn mul_assign(&mut self, rhs
: Rhs
);
883 macro_rules
! mul_assign_impl
{
885 #[stable(feature = "op_assign_traits", since = "1.8.0")]
886 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
887 impl const MulAssign
for $t
{
889 #[rustc_inherit_overflow_checks]
890 fn mul_assign(&mut self, other
: $t
) { *self *= other }
893 forward_ref_op_assign
! { impl const MulAssign, mul_assign for $t, $t }
897 mul_assign_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
899 /// The division assignment operator `/=`.
904 /// use std::ops::DivAssign;
906 /// #[derive(Debug, PartialEq)]
907 /// struct Frequency { hertz: f64 }
909 /// impl DivAssign<f64> for Frequency {
910 /// fn div_assign(&mut self, rhs: f64) {
911 /// self.hertz /= rhs;
915 /// let mut frequency = Frequency { hertz: 200.0 };
916 /// frequency /= 4.0;
917 /// assert_eq!(Frequency { hertz: 50.0 }, frequency);
919 #[lang = "div_assign"]
920 #[stable(feature = "op_assign_traits", since = "1.8.0")]
921 #[rustc_on_unimplemented(
922 message
= "cannot divide-assign `{Self}` by `{Rhs}`",
923 label
= "no implementation for `{Self} /= {Rhs}`"
928 pub trait DivAssign
<Rhs
= Self> {
929 /// Performs the `/=` operation.
934 /// let mut x: u32 = 12;
936 /// assert_eq!(x, 6);
938 #[stable(feature = "op_assign_traits", since = "1.8.0")]
939 fn div_assign(&mut self, rhs
: Rhs
);
942 macro_rules
! div_assign_impl
{
944 #[stable(feature = "op_assign_traits", since = "1.8.0")]
945 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
946 impl const DivAssign
for $t
{
948 fn div_assign(&mut self, other
: $t
) { *self /= other }
951 forward_ref_op_assign
! { impl const DivAssign, div_assign for $t, $t }
955 div_assign_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
957 /// The remainder assignment operator `%=`.
962 /// use std::ops::RemAssign;
964 /// struct CookieJar { cookies: u32 }
966 /// impl RemAssign<u32> for CookieJar {
967 /// fn rem_assign(&mut self, piles: u32) {
968 /// self.cookies %= piles;
972 /// let mut jar = CookieJar { cookies: 31 };
975 /// println!("Splitting up {} cookies into {} even piles!", jar.cookies, piles);
979 /// println!("{} cookies remain in the cookie jar!", jar.cookies);
981 #[lang = "rem_assign"]
982 #[stable(feature = "op_assign_traits", since = "1.8.0")]
983 #[rustc_on_unimplemented(
984 message
= "cannot mod-assign `{Self}` by `{Rhs}``",
985 label
= "no implementation for `{Self} %= {Rhs}`"
990 pub trait RemAssign
<Rhs
= Self> {
991 /// Performs the `%=` operation.
996 /// let mut x: u32 = 12;
998 /// assert_eq!(x, 2);
1000 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1001 fn rem_assign(&mut self, rhs
: Rhs
);
1004 macro_rules
! rem_assign_impl
{
1006 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1007 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
1008 impl const RemAssign
for $t
{
1010 fn rem_assign(&mut self, other
: $t
) { *self %= other }
1013 forward_ref_op_assign
! { impl const RemAssign, rem_assign for $t, $t }
1017 rem_assign_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }