Skip to content

Commit

Permalink
Rollup merge of rust-lang#35830 - matthew-piziak:not-example, r=steve…
Browse files Browse the repository at this point in the history
…klabnik

replace `Neg` example with something more evocative of negation
  • Loading branch information
Jonathan Turner committed Aug 20, 2016
2 parents 0090803 + c0eccb1 commit ce6ae56
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit ce6ae56

Please sign in to comment.