October 2018
Beginner
180 pages
4h 48m
English
These traits enable the +=, *=, -=, and /= operators for the types that implement them.
They are similar to the Add, Sub, Mul, and Div traits, with the difference that their implementation functions take &mut self instead of plain self. Instead of consuming their left-side input, they have the ability to change its contained value.
All of these traits follow the same pattern, so here's an example implementation of AddAssign:
pub enum AddExample { One, Two, Three, Many,}impl AddAssign for AddExample { fn add_assign(&mut self, other: AddExample) { *self = match (&self, other) { (AddExample::One, AddExample::One) => AddExample::Two, (AddExample::One, AddExample::Two) => AddExample::Three, (AddExample::Two, ...Read now
Unlock full access