pub trait ToPrimitive {Show 14 methods
// Required methods fnto_i64(&self) ->Option<i64>; fnto_u64(&self) ->Option<u64>; // Provided methods fnto_isize(&self) ->Option<isize> { ... } fnto_i8(&self) ->Option<i8> { ... } fnto_i16(&self) ->Option<i16> { ... } fnto_i32(&self) ->Option<i32> { ... } fnto_i128(&self) ->Option<i128> { ... } fnto_usize(&self) ->Option<usize> { ... } fnto_u8(&self) ->Option<u8> { ... } fnto_u16(&self) ->Option<u16> { ... } fnto_u32(&self) ->Option<u32> { ... } fnto_u128(&self) ->Option<u128> { ... } fnto_f32(&self) ->Option<f32> { ... } fnto_f64(&self) ->Option<f64> { ... }}
Expand description
A generic trait for converting a value to a number.
A value can be represented by the target type when it lies withinthe range of scalars supported by the target type.For example, a negative integer cannot be represented by an unsignedinteger type, and ani64
with a very high magnitude might not beconvertible to ani32
.On the other hand, conversions with possible precision loss or truncationare admitted, like anf32
with a decimal part to an integer type, oreven a largef64
saturating tof32
infinity.
Required Methods§
Provided Methods§
Sourcefnto_isize(&self) ->Option<isize>
fnto_isize(&self) ->Option<isize>
Converts the value ofself
to anisize
. If the value cannot berepresented by anisize
, thenNone
is returned.
Sourcefnto_i8(&self) ->Option<i8>
fnto_i8(&self) ->Option<i8>
Converts the value ofself
to ani8
. If the value cannot berepresented by ani8
, thenNone
is returned.
Sourcefnto_i16(&self) ->Option<i16>
fnto_i16(&self) ->Option<i16>
Converts the value ofself
to ani16
. If the value cannot berepresented by ani16
, thenNone
is returned.
Sourcefnto_i32(&self) ->Option<i32>
fnto_i32(&self) ->Option<i32>
Converts the value ofself
to ani32
. If the value cannot berepresented by ani32
, thenNone
is returned.
Sourcefnto_i128(&self) ->Option<i128>
fnto_i128(&self) ->Option<i128>
Converts the value ofself
to ani128
. If the value cannot berepresented by ani128
(i64
under the default implementation), thenNone
is returned.
The default implementation converts throughto_i64()
. Types implementingthis trait should override this method if they can represent a greater range.
Sourcefnto_usize(&self) ->Option<usize>
fnto_usize(&self) ->Option<usize>
Converts the value ofself
to ausize
. If the value cannot berepresented by ausize
, thenNone
is returned.
Sourcefnto_u8(&self) ->Option<u8>
fnto_u8(&self) ->Option<u8>
Converts the value ofself
to au8
. If the value cannot berepresented by au8
, thenNone
is returned.
Sourcefnto_u16(&self) ->Option<u16>
fnto_u16(&self) ->Option<u16>
Converts the value ofself
to au16
. If the value cannot berepresented by au16
, thenNone
is returned.
Sourcefnto_u32(&self) ->Option<u32>
fnto_u32(&self) ->Option<u32>
Converts the value ofself
to au32
. If the value cannot berepresented by au32
, thenNone
is returned.
Sourcefnto_u128(&self) ->Option<u128>
fnto_u128(&self) ->Option<u128>
Converts the value ofself
to au128
. If the value cannot berepresented by au128
(u64
under the default implementation), thenNone
is returned.
The default implementation converts throughto_u64()
. Types implementingthis trait should override this method if they can represent a greater range.
Sourcefnto_f32(&self) ->Option<f32>
fnto_f32(&self) ->Option<f32>
Converts the value ofself
to anf32
. Overflows may map to positiveor negative inifinity, otherwiseNone
is returned if the value cannotbe represented by anf32
.
Sourcefnto_f64(&self) ->Option<f64>
fnto_f64(&self) ->Option<f64>
Converts the value ofself
to anf64
. Overflows may map to positiveor negative inifinity, otherwiseNone
is returned if the value cannotbe represented by anf64
.
The default implementation tries to convert throughto_i64()
, andfailing that throughto_u64()
. Types implementing this trait shouldoverride this method if they can represent a greater range.