# [−] [src] Trait num_traits::cast::AsPrimitive

    pub trait AsPrimitive<T>: 'static + Copy where
        T: 'static + Copy, {
        fn as_(self) -> T;
    }

A generic interface for casting between machine scalars with the `as` operator, which admits narrowing and precision loss. Implementers of this trait AsPrimitive should behave like a primitive numeric type (e.g. a newtype around another primitive), and the intended conversion must never fail.

# Examples

    let three: i32 = (3.14159265f32).as_();
    assert_eq!(three, 3);

# Safety

Currently, some uses of the `as` operator are not entirely safe. In particular, it is undefined behavior if:

- A truncated floating point value cannot fit in the target integer type (#10184);

ⓘThis example is not tested

    let x: u8 = (1.04E+17).as_(); // UB

- Or a floating point value does not fit in another floating point type (#15536).

ⓘThis example is not tested

    let x: f32 = (1e300f64).as_(); // UB

## Required methods

### `fn as_(self) -> T`

Convert a value to another, using the `as` operator.

Loading content...

## Implementations on Foreign Types

### `impl AsPrimitive<char> for u8` [src]

#### `fn as_(self) -> char` [src]

### `impl AsPrimitive<f32> for u8` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for u8` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for u8` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for u8` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for u8` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for u8` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for u8` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for u8` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for u8` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for u8` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for u8` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for u8` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for u8` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for u8` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for i8` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for i8` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for i8` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for i8` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for i8` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for i8` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for i8` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for i8` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for i8` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for i8` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for i8` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for i8` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for i8` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for i8` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for u16` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for u16` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for u16` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for u16` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for u16` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for u16` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for u16` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for u16` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for u16` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for u16` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for u16` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for u16` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for u16` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for u16` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for i16` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for i16` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for i16` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for i16` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for i16` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for i16` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for i16` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for i16` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for i16` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for i16` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for i16` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for i16` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for i16` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for i16` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for u32` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for u32` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for u32` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for u32` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for u32` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for u32` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for u32` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for u32` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for u32` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for u32` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for u32` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for u32` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for u32` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for u32` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for i32` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for i32` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for i32` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for i32` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for i32` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for i32` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for i32` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for i32` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for i32` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for i32` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for i32` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for i32` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for i32` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for i32` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for u64` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for u64` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for u64` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for u64` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for u64` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for u64` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for u64` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for u64` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for u64` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for u64` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for u64` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for u64` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for u64` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for u64` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for i64` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for i64` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for i64` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for i64` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for i64` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for i64` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for i64` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for i64` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for i64` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for i64` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for i64` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for i64` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for i64` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for i64` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for u128` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for u128` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for u128` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for u128` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for u128` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for u128` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for u128` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for u128` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for u128` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for u128` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for u128` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for u128` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for u128` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for u128` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for i128` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for i128` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for i128` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for i128` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for i128` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for i128` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for i128` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for i128` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for i128` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for i128` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for i128` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for i128` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for i128` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for i128` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for usize` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for usize` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for usize` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for usize` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for usize` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for usize` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for usize` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for usize` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for usize` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for usize` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for usize` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for usize` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for usize` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for usize` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for isize` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for isize` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for isize` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for isize` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for isize` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for isize` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for isize` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for isize` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for isize` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for isize` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for isize` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for isize` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for isize` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for isize` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for f32` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for f32` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for f32` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for f32` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for f32` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for f32` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for f32` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for f32` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for f32` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for f32` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for f32` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for f32` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for f32` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for f32` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<f32> for f64` [src]

#### `fn as_(self) -> f32` [src]

### `impl AsPrimitive<f64> for f64` [src]

#### `fn as_(self) -> f64` [src]

### `impl AsPrimitive<u8> for f64` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for f64` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for f64` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for f64` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for f64` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for f64` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for f64` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for f64` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for f64` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for f64` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for f64` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for f64` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<char> for char` [src]

#### `fn as_(self) -> char` [src]

### `impl AsPrimitive<u8> for char` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for char` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for char` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for char` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for char` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for char` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for char` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for char` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for char` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for char` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for char` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for char` [src]

#### `fn as_(self) -> i128` [src]

### `impl AsPrimitive<u8> for bool` [src]

#### `fn as_(self) -> u8` [src]

### `impl AsPrimitive<u16> for bool` [src]

#### `fn as_(self) -> u16` [src]

### `impl AsPrimitive<u32> for bool` [src]

#### `fn as_(self) -> u32` [src]

### `impl AsPrimitive<u64> for bool` [src]

#### `fn as_(self) -> u64` [src]

### `impl AsPrimitive<usize> for bool` [src]

#### `fn as_(self) -> usize` [src]

### `impl AsPrimitive<u128> for bool` [src]

#### `fn as_(self) -> u128` [src]

### `impl AsPrimitive<i8> for bool` [src]

#### `fn as_(self) -> i8` [src]

### `impl AsPrimitive<i16> for bool` [src]

#### `fn as_(self) -> i16` [src]

### `impl AsPrimitive<i32> for bool` [src]

#### `fn as_(self) -> i32` [src]

### `impl AsPrimitive<i64> for bool` [src]

#### `fn as_(self) -> i64` [src]

### `impl AsPrimitive<isize> for bool` [src]

#### `fn as_(self) -> isize` [src]

### `impl AsPrimitive<i128> for bool` [src]

#### `fn as_(self) -> i128` [src]

Loading content...

## Implementors

Loading content...

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse 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`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)