Trait std::ops::Neg1.0.0 [] [src]

pub trait Neg {
    type Output;
    fn neg(self) -> Self::Output;
}

The Neg trait is used to specify the functionality of unary -.

Examples

An implementation of Neg for Sign, which allows the use of - to negate its value.

use std::ops::Neg;

#[derive(Debug, PartialEq)]
enum Sign {
    Negative,
    Zero,
    Positive,
}

impl Neg for Sign {
    type Output = Sign;

    fn neg(self) -> Sign {
        match self {
            Sign::Negative => Sign::Positive,
            Sign::Zero => Sign::Zero,
            Sign::Positive => Sign::Negative,
        }
    }
}

// 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);Run

Associated Types

The resulting type after applying the - operator

Required Methods

The method for the unary - operator

Implementors