std::ops::Neg - Rust
Description: API documentation for the Rust `Neg` trait in crate `std`.
Keywords: rust, rustlang, rust-lang, Neg

# Trait std::ops::Neg 1.0.0 [−] [src]

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

## Associated Types

### `type Output`

The resulting type after applying the `-` operator

## Required Methods

### `fn neg(self) -> Self::Output`

The method for the unary `-` operator

## Implementors

- `impl Neg for std::num::Wrapping<usize>`
- `impl Neg for std::num::Wrapping<u8>`
- `impl Neg for std::num::Wrapping<u16>`
- `impl Neg for std::num::Wrapping<u32>`
- `impl Neg for std::num::Wrapping<u64>`
- `impl Neg for std::num::Wrapping<isize>`
- `impl Neg for std::num::Wrapping<i8>`
- `impl Neg for std::num::Wrapping<i16>`
- `impl Neg for std::num::Wrapping<i32>`
- `impl Neg for std::num::Wrapping<i64>`
- `impl<'a> Neg for &'a Wrapping<usize>`
- `impl<'a> Neg for &'a Wrapping<u8>`
- `impl<'a> Neg for &'a Wrapping<u16>`
- `impl<'a> Neg for &'a Wrapping<u32>`
- `impl<'a> Neg for &'a Wrapping<u64>`
- `impl<'a> Neg for &'a Wrapping<isize>`
- `impl<'a> Neg for &'a Wrapping<i8>`
- `impl<'a> Neg for &'a Wrapping<i16>`
- `impl<'a> Neg for &'a Wrapping<i32>`
- `impl<'a> Neg for &'a Wrapping<i64>`
- `impl Neg for std::num::Wrapping<u128>`
- `impl Neg for std::num::Wrapping<i128>`
- `impl<'a> Neg for &'a Wrapping<u128>`
- `impl<'a> Neg for &'a Wrapping<i128>`
- `impl Neg for isize`
- `impl Neg for i8`
- `impl Neg for i16`
- `impl Neg for i32`
- `impl Neg for i64`
- `impl Neg for f32`
- `impl Neg for f64`
- `impl<'a> Neg for &'a isize`
- `impl<'a> Neg for &'a i8`
- `impl<'a> Neg for &'a i16`
- `impl<'a> Neg for &'a i32`
- `impl<'a> Neg for &'a i64`
- `impl<'a> Neg for &'a f32`
- `impl<'a> Neg for &'a f64`
- `impl Neg for i128`
- `impl<'a> Neg for &'a i128`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)