Trait num_traits::cast::AsPrimitive [−][src]
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.
Implementors
impl AsPrimitive<char> for u8
impl AsPrimitive<f32> for u8
impl AsPrimitive<f64> for u8
impl AsPrimitive<u8> for u8
impl AsPrimitive<u16> for u8
impl AsPrimitive<u32> for u8
impl AsPrimitive<u64> for u8
impl AsPrimitive<usize> for u8
impl AsPrimitive<u128> for u8
impl AsPrimitive<i8> for u8
impl AsPrimitive<i16> for u8
impl AsPrimitive<i32> for u8
impl AsPrimitive<i64> for u8
impl AsPrimitive<isize> for u8
impl AsPrimitive<i128> for u8
impl AsPrimitive<f32> for i8
impl AsPrimitive<f64> for i8
impl AsPrimitive<u8> for i8
impl AsPrimitive<u16> for i8
impl AsPrimitive<u32> for i8
impl AsPrimitive<u64> for i8
impl AsPrimitive<usize> for i8
impl AsPrimitive<u128> for i8
impl AsPrimitive<i8> for i8
impl AsPrimitive<i16> for i8
impl AsPrimitive<i32> for i8
impl AsPrimitive<i64> for i8
impl AsPrimitive<isize> for i8
impl AsPrimitive<i128> for i8
impl AsPrimitive<f32> for u16
impl AsPrimitive<f64> for u16
impl AsPrimitive<u8> for u16
impl AsPrimitive<u16> for u16
impl AsPrimitive<u32> for u16
impl AsPrimitive<u64> for u16
impl AsPrimitive<usize> for u16
impl AsPrimitive<u128> for u16
impl AsPrimitive<i8> for u16
impl AsPrimitive<i16> for u16
impl AsPrimitive<i32> for u16
impl AsPrimitive<i64> for u16
impl AsPrimitive<isize> for u16
impl AsPrimitive<i128> for u16
impl AsPrimitive<f32> for i16
impl AsPrimitive<f64> for i16
impl AsPrimitive<u8> for i16
impl AsPrimitive<u16> for i16
impl AsPrimitive<u32> for i16
impl AsPrimitive<u64> for i16
impl AsPrimitive<usize> for i16
impl AsPrimitive<u128> for i16
impl AsPrimitive<i8> for i16
impl AsPrimitive<i16> for i16
impl AsPrimitive<i32> for i16
impl AsPrimitive<i64> for i16
impl AsPrimitive<isize> for i16
impl AsPrimitive<i128> for i16
impl AsPrimitive<f32> for u32
impl AsPrimitive<f64> for u32
impl AsPrimitive<u8> for u32
impl AsPrimitive<u16> for u32
impl AsPrimitive<u32> for u32
impl AsPrimitive<u64> for u32
impl AsPrimitive<usize> for u32
impl AsPrimitive<u128> for u32
impl AsPrimitive<i8> for u32
impl AsPrimitive<i16> for u32
impl AsPrimitive<i32> for u32
impl AsPrimitive<i64> for u32
impl AsPrimitive<isize> for u32
impl AsPrimitive<i128> for u32
impl AsPrimitive<f32> for i32
impl AsPrimitive<f64> for i32
impl AsPrimitive<u8> for i32
impl AsPrimitive<u16> for i32
impl AsPrimitive<u32> for i32
impl AsPrimitive<u64> for i32
impl AsPrimitive<usize> for i32
impl AsPrimitive<u128> for i32
impl AsPrimitive<i8> for i32
impl AsPrimitive<i16> for i32
impl AsPrimitive<i32> for i32
impl AsPrimitive<i64> for i32
impl AsPrimitive<isize> for i32
impl AsPrimitive<i128> for i32
impl AsPrimitive<f32> for u64
impl AsPrimitive<f64> for u64
impl AsPrimitive<u8> for u64
impl AsPrimitive<u16> for u64
impl AsPrimitive<u32> for u64
impl AsPrimitive<u64> for u64
impl AsPrimitive<usize> for u64
impl AsPrimitive<u128> for u64
impl AsPrimitive<i8> for u64
impl AsPrimitive<i16> for u64
impl AsPrimitive<i32> for u64
impl AsPrimitive<i64> for u64
impl AsPrimitive<isize> for u64
impl AsPrimitive<i128> for u64
impl AsPrimitive<f32> for i64
impl AsPrimitive<f64> for i64
impl AsPrimitive<u8> for i64
impl AsPrimitive<u16> for i64
impl AsPrimitive<u32> for i64
impl AsPrimitive<u64> for i64
impl AsPrimitive<usize> for i64
impl AsPrimitive<u128> for i64
impl AsPrimitive<i8> for i64
impl AsPrimitive<i16> for i64
impl AsPrimitive<i32> for i64
impl AsPrimitive<i64> for i64
impl AsPrimitive<isize> for i64
impl AsPrimitive<i128> for i64
impl AsPrimitive<f32> for u128
impl AsPrimitive<f64> for u128
impl AsPrimitive<u8> for u128
impl AsPrimitive<u16> for u128
impl AsPrimitive<u32> for u128
impl AsPrimitive<u64> for u128
impl AsPrimitive<usize> for u128
impl AsPrimitive<u128> for u128
impl AsPrimitive<i8> for u128
impl AsPrimitive<i16> for u128
impl AsPrimitive<i32> for u128
impl AsPrimitive<i64> for u128
impl AsPrimitive<isize> for u128
impl AsPrimitive<i128> for u128
impl AsPrimitive<f32> for i128
impl AsPrimitive<f64> for i128
impl AsPrimitive<u8> for i128
impl AsPrimitive<u16> for i128
impl AsPrimitive<u32> for i128
impl AsPrimitive<u64> for i128
impl AsPrimitive<usize> for i128
impl AsPrimitive<u128> for i128
impl AsPrimitive<i8> for i128
impl AsPrimitive<i16> for i128
impl AsPrimitive<i32> for i128
impl AsPrimitive<i64> for i128
impl AsPrimitive<isize> for i128
impl AsPrimitive<i128> for i128
impl AsPrimitive<f32> for usize
impl AsPrimitive<f64> for usize
impl AsPrimitive<u8> for usize
impl AsPrimitive<u16> for usize
impl AsPrimitive<u32> for usize
impl AsPrimitive<u64> for usize
impl AsPrimitive<usize> for usize
impl AsPrimitive<u128> for usize
impl AsPrimitive<i8> for usize
impl AsPrimitive<i16> for usize
impl AsPrimitive<i32> for usize
impl AsPrimitive<i64> for usize
impl AsPrimitive<isize> for usize
impl AsPrimitive<i128> for usize
impl AsPrimitive<f32> for isize
impl AsPrimitive<f64> for isize
impl AsPrimitive<u8> for isize
impl AsPrimitive<u16> for isize
impl AsPrimitive<u32> for isize
impl AsPrimitive<u64> for isize
impl AsPrimitive<usize> for isize
impl AsPrimitive<u128> for isize
impl AsPrimitive<i8> for isize
impl AsPrimitive<i16> for isize
impl AsPrimitive<i32> for isize
impl AsPrimitive<i64> for isize
impl AsPrimitive<isize> for isize
impl AsPrimitive<i128> for isize
impl AsPrimitive<f32> for f32
impl AsPrimitive<f64> for f32
impl AsPrimitive<u8> for f32
impl AsPrimitive<u16> for f32
impl AsPrimitive<u32> for f32
impl AsPrimitive<u64> for f32
impl AsPrimitive<usize> for f32
impl AsPrimitive<u128> for f32
impl AsPrimitive<i8> for f32
impl AsPrimitive<i16> for f32
impl AsPrimitive<i32> for f32
impl AsPrimitive<i64> for f32
impl AsPrimitive<isize> for f32
impl AsPrimitive<i128> for f32
impl AsPrimitive<f32> for f64
impl AsPrimitive<f64> for f64
impl AsPrimitive<u8> for f64
impl AsPrimitive<u16> for f64
impl AsPrimitive<u32> for f64
impl AsPrimitive<u64> for f64
impl AsPrimitive<usize> for f64
impl AsPrimitive<u128> for f64
impl AsPrimitive<i8> for f64
impl AsPrimitive<i16> for f64
impl AsPrimitive<i32> for f64
impl AsPrimitive<i64> for f64
impl AsPrimitive<isize> for f64
impl AsPrimitive<i128> for f64
impl AsPrimitive<char> for char
impl AsPrimitive<u8> for char
impl AsPrimitive<u16> for char
impl AsPrimitive<u32> for char
impl AsPrimitive<u64> for char
impl AsPrimitive<usize> for char
impl AsPrimitive<u128> for char
impl AsPrimitive<i8> for char
impl AsPrimitive<i16> for char
impl AsPrimitive<i32> for char
impl AsPrimitive<i64> for char
impl AsPrimitive<isize> for char
impl AsPrimitive<i128> for char
impl AsPrimitive<u8> for bool
impl AsPrimitive<u16> for bool
impl AsPrimitive<u32> for bool
impl AsPrimitive<u64> for bool
impl AsPrimitive<usize> for bool
impl AsPrimitive<u128> for bool
impl AsPrimitive<i8> for bool
impl AsPrimitive<i16> for bool
impl AsPrimitive<i32> for bool
impl AsPrimitive<i64> for bool
impl AsPrimitive<isize> for bool
impl AsPrimitive<i128> for bool