W3cubDocs

/Rust

Trait std::ops::Neg

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

Associated Types

The resulting type after applying the - operator

Required Methods

The method for the unary - operator

Implementors

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ops/trait.Neg.html