File tree 1 file changed +22
-11
lines changed
1 file changed +22
-11
lines changed Original file line number Diff line number Diff line change @@ -556,26 +556,37 @@ rem_impl_float! { f32 f64 }
556
556
///
557
557
/// # Examples
558
558
///
559
- /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
560
- /// `neg`, and therefore, `main` prints `Negating!` .
559
+ /// An implementation of `Neg` for `Sign`, which allows the use of `-` to
560
+ /// negate its value .
561
561
///
562
562
/// ```
563
563
/// use std::ops::Neg;
564
564
///
565
- /// struct Foo;
565
+ /// #[derive(Debug, PartialEq)]
566
+ /// enum Sign {
567
+ /// Negative,
568
+ /// Zero,
569
+ /// Positive,
570
+ /// }
566
571
///
567
- /// impl Neg for Foo {
568
- /// type Output = Foo ;
572
+ /// impl Neg for Sign {
573
+ /// type Output = Sign ;
569
574
///
570
- /// fn neg(self) -> Foo {
571
- /// println!("Negating!");
572
- /// self
575
+ /// fn neg(self) -> Sign {
576
+ /// match self {
577
+ /// Sign::Negative => Sign::Positive,
578
+ /// Sign::Zero => Sign::Zero,
579
+ /// Sign::Positive => Sign::Negative,
580
+ /// }
573
581
/// }
574
582
/// }
575
583
///
576
- /// fn main() {
577
- /// -Foo;
578
- /// }
584
+ /// // a negative positive is a negative
585
+ /// assert_eq!(-Sign::Positive, Sign::Negative);
586
+ /// // a double negative is a positive
587
+ /// assert_eq!(-Sign::Negative, Sign::Positive);
588
+ /// // zero is its own negation
589
+ /// assert_eq!(-Sign::Zero, Sign::Zero);
579
590
/// ```
580
591
#[ lang = "neg" ]
581
592
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
You can’t perform that action at this time.
0 commit comments