about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonathan Turner <jonathandturner@users.noreply.github.com>2016-08-20 07:09:37 -0700
committerGitHub <noreply@github.com>2016-08-20 07:09:37 -0700
commit0d69b887c698a8189aa3438544ea6ad4f1336d4d (patch)
treefb5e120361700d8c5cea3fd1fb48dcf0b19b2578
parentcc431a017d5c35e1376a33fb443c12e21d1139cc (diff)
parentc0eccb120326bc559a4cbe30ace4935d83420073 (diff)
Rollup merge of #35830 - matthew-piziak:not-example, r=steveklabnik
replace `Neg` example with something more evocative of negation
-rw-r--r--src/libcore/ops.rs33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs
index 86439eca9e5..5a1993e741c 100644
--- a/src/libcore/ops.rs
+++ b/src/libcore/ops.rs
@@ -556,26 +556,37 @@ rem_impl_float! { f32 f64 }
 ///
 /// # Examples
 ///
-/// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
-/// `neg`, and therefore, `main` prints `Negating!`.
+/// An implementation of `Neg` for `Sign`, which allows the use of `-` to
+/// negate its value.
 ///
 /// ```
 /// use std::ops::Neg;
 ///
-/// struct Foo;
+/// #[derive(Debug, PartialEq)]
+/// enum Sign {
+///     Negative,
+///     Zero,
+///     Positive,
+/// }
 ///
-/// impl Neg for Foo {
-///     type Output = Foo;
+/// impl Neg for Sign {
+///     type Output = Sign;
 ///
-///     fn neg(self) -> Foo {
-///         println!("Negating!");
-///         self
+///     fn neg(self) -> Sign {
+///         match self {
+///             Sign::Negative => Sign::Positive,
+///             Sign::Zero => Sign::Zero,
+///             Sign::Positive => Sign::Negative,
+///         }
 ///     }
 /// }
 ///
-/// fn main() {
-///     -Foo;
-/// }
+/// // a negative positive is a negative
+/// assert_eq!(-Sign::Positive, Sign::Negative);
+/// // a double negative is a positive
+/// assert_eq!(-Sign::Negative, Sign::Positive);
+/// // zero is its own negation
+/// assert_eq!(-Sign::Zero, Sign::Zero);
 /// ```
 #[lang = "neg"]
 #[stable(feature = "rust1", since = "1.0.0")]