std::ops::Neg

Trait std::ops::Neg

#[lang = "neg"]
pub trait Neg {
    type Output;
    fn neg(self) -> Self::Output;
}

The unary negation operator -.

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

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部