CheckedNeg in num_traits::ops::checked - Rust
Description: Performs negation that returns `None` if the result can’t be represented.
Keywords: rust, rustlang, rust-lang, CheckedNeg

# Trait num_traits::ops::checked::CheckedNeg

source ·

    pub trait CheckedNeg: Sized {
        fn checked_neg(&self) -> Option<Self>;
    }

Expand description

Performs negation that returns `None` if the result can’t be represented.

## Required Methods§

source

#### fn checked_neg(&self) -> Option<Self>

Negates a number, returning `None` for results that can’t be represented, like signed `MIN` values that can’t be positive, or non-zero unsigned values that can’t be negative.

##### Examples

    use num_traits::CheckedNeg;
    use std::i32::MIN;

    assert_eq!(CheckedNeg::checked_neg(&1_i32), Some(-1));
    assert_eq!(CheckedNeg::checked_neg(&-1_i32), Some(1));
    assert_eq!(CheckedNeg::checked_neg(&MIN), None);

    assert_eq!(CheckedNeg::checked_neg(&0_u32), Some(0));
    assert_eq!(CheckedNeg::checked_neg(&1_u32), None);

## Implementations on Foreign Types§

source§

### impl CheckedNeg for u8

source§

#### fn checked_neg(&self) -> Option<u8>

source§

### impl CheckedNeg for u16

source§

#### fn checked_neg(&self) -> Option<u16>

source§

### impl CheckedNeg for u32

source§

#### fn checked_neg(&self) -> Option<u32>

source§

### impl CheckedNeg for u64

source§

#### fn checked_neg(&self) -> Option<u64>

source§

### impl CheckedNeg for usize

source§

#### fn checked_neg(&self) -> Option<usize>

source§

### impl CheckedNeg for u128

source§

#### fn checked_neg(&self) -> Option<u128>

source§

### impl CheckedNeg for i8

source§

#### fn checked_neg(&self) -> Option<i8>

source§

### impl CheckedNeg for i16

source§

#### fn checked_neg(&self) -> Option<i16>

source§

### impl CheckedNeg for i32

source§

#### fn checked_neg(&self) -> Option<i32>

source§

### impl CheckedNeg for i64

source§

#### fn checked_neg(&self) -> Option<i64>

source§

### impl CheckedNeg for isize

source§

#### fn checked_neg(&self) -> Option<isize>

source§

### impl CheckedNeg for i128

source§

#### fn checked_neg(&self) -> Option<i128>

## Implementors§