Trait ToPrimitive
trait ToPrimitive
A generic trait for converting a value to a number.
A value can be represented by the target type when it lies within
the range of scalars supported by the target type.
For example, a negative integer cannot be represented by an unsigned
integer type, and an i64 with a very high magnitude might not be
convertible to an i32.
On the other hand, conversions with possible precision loss or truncation
are admitted, like an f32 with a decimal part to an integer type, or
even a large f64 saturating to f32 infinity.
Required Methods
fn to_i64(self: &Self) -> Option<i64>Converts the value of
selfto ani64. If the value cannot be represented by ani64, thenNoneis returned.fn to_u64(self: &Self) -> Option<u64>Converts the value of
selfto au64. If the value cannot be represented by au64, thenNoneis returned.
Provided Methods
fn to_isize(self: &Self) -> Option<isize>Converts the value of
selfto anisize. If the value cannot be represented by anisize, thenNoneis returned.fn to_i8(self: &Self) -> Option<i8>Converts the value of
selfto ani8. If the value cannot be represented by ani8, thenNoneis returned.fn to_i16(self: &Self) -> Option<i16>Converts the value of
selfto ani16. If the value cannot be represented by ani16, thenNoneis returned.fn to_i32(self: &Self) -> Option<i32>Converts the value of
selfto ani32. If the value cannot be represented by ani32, thenNoneis returned.fn to_i128(self: &Self) -> Option<i128>Converts the value of
selfto ani128. If the value cannot be represented by ani128(i64under the default implementation), thenNoneis returned.The default implementation converts through
to_i64(). Types implementing this trait should override this method if they can represent a greater range.fn to_usize(self: &Self) -> Option<usize>Converts the value of
selfto ausize. If the value cannot be represented by ausize, thenNoneis returned.fn to_u8(self: &Self) -> Option<u8>Converts the value of
selfto au8. If the value cannot be represented by au8, thenNoneis returned.fn to_u16(self: &Self) -> Option<u16>Converts the value of
selfto au16. If the value cannot be represented by au16, thenNoneis returned.fn to_u32(self: &Self) -> Option<u32>Converts the value of
selfto au32. If the value cannot be represented by au32, thenNoneis returned.fn to_u128(self: &Self) -> Option<u128>Converts the value of
selfto au128. If the value cannot be represented by au128(u64under the default implementation), thenNoneis returned.The default implementation converts through
to_u64(). Types implementing this trait should override this method if they can represent a greater range.fn to_f32(self: &Self) -> Option<f32>Converts the value of
selfto anf32. Overflows may map to positive or negative inifinity, otherwiseNoneis returned if the value cannot be represented by anf32.fn to_f64(self: &Self) -> Option<f64>Converts the value of
selfto anf64. Overflows may map to positive or negative inifinity, otherwiseNoneis returned if the value cannot be represented by anf64.The default implementation tries to convert through
to_i64(), and failing that throughto_u64(). Types implementing this trait should override this method if they can represent a greater range.
Implementors
impl ToPrimitive for i16impl ToPrimitive for i32impl ToPrimitive for i64impl ToPrimitive for i128impl ToPrimitive for usizeimpl ToPrimitive for u8impl ToPrimitive for u16impl ToPrimitive for u32impl ToPrimitive for u64impl ToPrimitive for u128impl ToPrimitive for f32impl ToPrimitive for f64impl<T: ToPrimitive> ToPrimitive for Wrapping<T>impl ToPrimitive for isizeimpl ToPrimitive for i8