October 2018
Beginner
180 pages
4h 48m
English
The Add, Mul, Sub, and Div traits represent the ability to add, multiply, subtract, or divide two values. These traits are used by the compiler to implement the +, *, -, and / operators.
Notice that if the values of self and other do not have the Copy trait, they are moved into the implementation function and consumed.
All of these traits follow the same pattern, so here's an example implementation of Add:
pub enum AddExample { One, Two, Three, Many,}impl Add for AddExample { type Output = AddExample; fn add(self, other: AddExample) -> AddExample { match (self, other) { (AddExample::One, AddExample::One) => AddExample::Two, (AddExample::One, AddExample::Two) => AddExample::Three, (AddExample::Two, AddExample::One) ...Read now
Unlock full access