Skip to content

Commit 2afa054

Browse files
author
Jonathan Turner
authored
Rollup merge of #35830 - matthew-piziak:not-example, r=steveklabnik
replace `Neg` example with something more evocative of negation
2 parents 063e01c + c0eccb1 commit 2afa054

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

Diff for: src/libcore/ops.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -556,26 +556,37 @@ rem_impl_float! { f32 f64 }
556556
///
557557
/// # Examples
558558
///
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.
561561
///
562562
/// ```
563563
/// use std::ops::Neg;
564564
///
565-
/// struct Foo;
565+
/// #[derive(Debug, PartialEq)]
566+
/// enum Sign {
567+
/// Negative,
568+
/// Zero,
569+
/// Positive,
570+
/// }
566571
///
567-
/// impl Neg for Foo {
568-
/// type Output = Foo;
572+
/// impl Neg for Sign {
573+
/// type Output = Sign;
569574
///
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+
/// }
573581
/// }
574582
/// }
575583
///
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);
579590
/// ```
580591
#[lang = "neg"]
581592
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)