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

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:

This example is not tested
let x: u8 = (1.04E+17).as_(); // UB
This example is not tested
let x: f32 = (1e300f64).as_(); // UB

Required Methods

Convert a value to another, using the as operator.

Implementors