October 2018
Beginner
180 pages
4h 48m
English
The Neg trait enables a data type to be used with the unary negation operator, also known as the negative sign. When we write -5, we're applying the unary negation operator to the value 5, producing a negative 5 as the result.
Implementing Neg looks like this:
pub enum NegExample { Yes, No,}impl Neg for NegExample { type Output = NegExample; fn neg(self) -> NegExample { match self { NegExample::Yes => NegExample::No, NegExample::No => NegExample::Yes, } }}
Read now
Unlock full access