Skip to content

Commit c0eccb1

Browse files
replace Neg example with something more evocative of negation
1 parent 11f8805 commit c0eccb1

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/libcore/ops.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -470,26 +470,37 @@ rem_impl_float! { f32 f64 }
470470
///
471471
/// # Examples
472472
///
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.
475475
///
476476
/// ```
477477
/// use std::ops::Neg;
478478
///
479-
/// struct Foo;
479+
/// #[derive(Debug, PartialEq)]
480+
/// enum Sign {
481+
/// Negative,
482+
/// Zero,
483+
/// Positive,
484+
/// }
480485
///
481-
/// impl Neg for Foo {
482-
/// type Output = Foo;
486+
/// impl Neg for Sign {
487+
/// type Output = Sign;
483488
///
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+
/// }
487495
/// }
488496
/// }
489497
///
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);
493504
/// ```
494505
#[lang = "neg"]
495506
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)