The unary negation operator -.

Signature

pub trait Neg

Examples

An implementation of Neg for Sign, which allows the use of - to negate its value.
#[derive(Copy, Drop, PartialEq)]
enum Sign {
    Negative,
    Zero,
    Positive,
}

impl SignNeg of Neg {
    fn neg(a: Sign) -> Sign {
        match a {
            Sign::Negative => Sign::Positive,
            Sign::Zero => Sign::Zero,
            Sign::Positive => Sign::Negative,
        }
    }
}

// A negative positive is a negative
assert!(-Sign::Positive == Sign::Negative);
// A double negative is a positive
assert!(-Sign::Negative == Sign::Positive);
// Zero is its own negation
assert!(-Sign::Zero == Sign::Zero);

Trait functions

neg

Performs the unary - operation.

Signature

fn neg(a: T) -> T

Examples

let x: i8 = 1;
assert!(-x == -1);