Neg
Neg (단항 부정 연산자 트레이트)
단항 부정 연산자 -를 위한 트레이트예요.
출처: Rust 공식 문서
본문
값의 부호를 뒤집는 단항 연산에 사용돼요.
pub trait Neg {
type Output;
// 필수 메서드
fn neg(self) -> Self::Output;
}
예시
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,
}
}
}