num::traits

Trait AsPrimitive

Source
pub trait AsPrimitive<T>: 'static + Copy
where T: 'static + Copy,
{ // Required method fn as_(self) -> T; }
Expand description

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

In Rust versions before 1.45.0, some uses of the as operator were not entirely safe. In particular, it was undefined behavior if a truncated floating point value could not fit in the target integer type (#10184).

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

Required Methods§

Source

fn as_(self) -> T

Convert a value to another, using the as operator.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl AsPrimitive<char> for char

Source§

fn as_(self) -> char

Source§

impl AsPrimitive<char> for u8

Source§

fn as_(self) -> char

Source§

impl AsPrimitive<f32> for f32

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for f64

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for i8

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for i16

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for i32

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for i64

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for i128

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for isize

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for u8

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for u16

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for u32

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for u64

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for u128

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f32> for usize

Source§

fn as_(self) -> f32

Source§

impl AsPrimitive<f64> for f32

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for f64

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for i8

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for i16

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for i32

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for i64

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for i128

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for isize

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for u8

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for u16

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for u32

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for u64

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for u128

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<f64> for usize

Source§

fn as_(self) -> f64

Source§

impl AsPrimitive<i8> for bool

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for char

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for f32

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for f64

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for i8

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for i16

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for i32

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for i64

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for i128

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for isize

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for u8

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for u16

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for u32

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for u64

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for u128

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i8> for usize

Source§

fn as_(self) -> i8

Source§

impl AsPrimitive<i16> for bool

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for char

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for f32

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for f64

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for i8

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for i16

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for i32

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for i64

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for i128

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for isize

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for u8

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for u16

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for u32

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for u64

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for u128

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i16> for usize

Source§

fn as_(self) -> i16

Source§

impl AsPrimitive<i32> for bool

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for char

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for f32

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for f64

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for i8

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for i16

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for i32

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for i64

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for i128

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for isize

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for u8

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for u16

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for u32

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for u64

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for u128

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i32> for usize

Source§

fn as_(self) -> i32

Source§

impl AsPrimitive<i64> for bool

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for char

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for f32

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for f64

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for i8

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for i16

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for i32

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for i64

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for i128

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for isize

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for u8

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for u16

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for u32

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for u64

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for u128

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i64> for usize

Source§

fn as_(self) -> i64

Source§

impl AsPrimitive<i128> for bool

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for char

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for f32

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for f64

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for i8

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for i16

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for i32

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for i64

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for i128

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for isize

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for u8

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for u16

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for u32

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for u64

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for u128

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<i128> for usize

Source§

fn as_(self) -> i128

Source§

impl AsPrimitive<isize> for bool

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for char

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for f32

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for f64

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for i8

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for i16

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for i32

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for i64

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for i128

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for isize

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for u8

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for u16

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for u32

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for u64

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for u128

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<isize> for usize

Source§

fn as_(self) -> isize

Source§

impl AsPrimitive<u8> for bool

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for char

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for f32

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for f64

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for i8

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for i16

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for i32

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for i64

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for i128

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for isize

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for u8

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for u16

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for u32

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for u64

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for u128

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u8> for usize

Source§

fn as_(self) -> u8

Source§

impl AsPrimitive<u16> for bool

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for char

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for f32

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for f64

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for i8

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for i16

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for i32

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for i64

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for i128

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for isize

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for u8

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for u16

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for u32

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for u64

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for u128

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u16> for usize

Source§

fn as_(self) -> u16

Source§

impl AsPrimitive<u32> for bool

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for char

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for f32

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for f64

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for i8

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for i16

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for i32

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for i64

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for i128

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for isize

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for u8

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for u16

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for u32

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for u64

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for u128

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u32> for usize

Source§

fn as_(self) -> u32

Source§

impl AsPrimitive<u64> for bool

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for char

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for f32

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for f64

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for i8

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for i16

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for i32

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for i64

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for i128

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for isize

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for u8

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for u16

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for u32

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for u64

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for u128

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u64> for usize

Source§

fn as_(self) -> u64

Source§

impl AsPrimitive<u128> for bool

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for char

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for f32

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for f64

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for i8

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for i16

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for i32

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for i64

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for i128

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for isize

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for u8

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for u16

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for u32

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for u64

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for u128

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<u128> for usize

Source§

fn as_(self) -> u128

Source§

impl AsPrimitive<usize> for bool

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for char

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for f32

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for f64

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for i8

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for i16

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for i32

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for i64

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for i128

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for isize

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for u8

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for u16

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for u32

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for u64

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for u128

Source§

fn as_(self) -> usize

Source§

impl AsPrimitive<usize> for usize

Source§

fn as_(self) -> usize

Implementors§

Source§

impl<T, U> AsPrimitive<U> for Complex<T>
where T: AsPrimitive<U>, U: 'static + Copy,