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

pub trait Neg {
    type Output;
    #[must_use]
    fn neg(self) -> Self::Output;
}
Expand description

一元求反运算符 -

Examples

SignNeg 实现,它允许使用 - 取反其值。

use std::ops::Neg;

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

impl Neg for Sign {
    type Output = Self;

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

// 负数肯定是负数。
assert_eq!(-Sign::Positive, Sign::Negative);
// 双重否定是积极的。
assert_eq!(-Sign::Negative, Sign::Positive);
// 零是它自己的否定。
assert_eq!(-Sign::Zero, Sign::Zero);
Run

Associated Types

应用 - 运算符后的结果类型。

Required methods

执行一元 - 操作。

Example

let x: i32 = 12;
assert_eq!(-x, -12);
Run

Implementors