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 @@ -470,26 +470,37 @@ rem_impl_float! { f32 f64 }
470
470
///
471
471
/// # Examples
472
472
///
473
- /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
474
- /// `neg`, and therefore, `main` prints `Negating!` .
473
+ /// An implementation of `Neg` for `Sign`, which allows the use of `-` to
474
+ /// negate its value .
475
475
///
476
476
/// ```
477
477
/// use std::ops::Neg;
478
478
///
479
- /// struct Foo;
479
+ /// #[derive(Debug, PartialEq)]
480
+ /// enum Sign {
481
+ /// Negative,
482
+ /// Zero,
483
+ /// Positive,
484
+ /// }
480
485
///
481
- /// impl Neg for Foo {
482
- /// type Output = Foo ;
486
+ /// impl Neg for Sign {
487
+ /// type Output = Sign ;
483
488
///
484
- /// fn neg(self) -> Foo {
485
- /// println!("Negating!");
486
- /// self
489
+ /// fn neg(self) -> Sign {
490
+ /// match self {
491
+ /// Sign::Negative => Sign::Positive,
492
+ /// Sign::Zero => Sign::Zero,
493
+ /// Sign::Positive => Sign::Negative,
494
+ /// }
487
495
/// }
488
496
/// }
489
497
///
490
- /// fn main() {
491
- /// -Foo;
492
- /// }
498
+ /// // a negative positive is a negative
499
+ /// assert_eq!(-Sign::Positive, Sign::Negative);
500
+ /// // a double negative is a positive
501
+ /// assert_eq!(-Sign::Negative, Sign::Positive);
502
+ /// // zero is its own negation
503
+ /// assert_eq!(-Sign::Zero, Sign::Zero);
493
504
/// ```
494
505
#[ lang = "neg" ]
495
506
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
You can’t perform that action at this time.
0 commit comments