pub trait Num: PartialEq<Self> + Zero + One + NumOps<Self, Self> {
type FromStrRadixErr;
fn from_str_radix(
str: &str,
radix: u32
) -> Result<Self, Self::FromStrRadixErr>;
}
Expand description
The base trait for numeric types, covering 0
and 1
values,
comparisons, basic numeric operations, and string conversion.
Required Associated Types
Required Methods
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
Convert from a string and radix (typically 2..=36
).
Examples
use num_traits::Num;
let result = <i32 as Num>::from_str_radix("27", 10);
assert_eq!(result, Ok(27));
let result = <i32 as Num>::from_str_radix("foo", 10);
assert!(result.is_err());
Supported radices
The exact range of supported radices is at the discretion of each type implementation. For
primitive integers, this is implemented by the inherent from_str_radix
methods in the
standard library, which panic if the radix is not in the range from 2 to 36. The
implementation in this crate for primitive floats is similar.
For third-party types, it is suggested that implementations should follow suit and at least
accept 2..=36
without panicking, but an Err
may be returned for any unsupported radix.
It’s possible that a type might not even support the common radix 10, nor any, if string
parsing doesn’t make sense for that type.
Implementations on Foreign Types
sourceimpl Num for i8
impl Num for i8
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<i8, ParseIntError>
sourceimpl Num for i32
impl Num for i32
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<i32, ParseIntError>
sourceimpl Num for i128
impl Num for i128
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<i128, ParseIntError>
sourceimpl Num for u16
impl Num for u16
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<u16, ParseIntError>
sourceimpl Num for i16
impl Num for i16
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<i16, ParseIntError>
sourceimpl Num for f32
impl Num for f32
type FromStrRadixErr = ParseFloatError
fn from_str_radix(
src: &str,
radix: u32
) -> Result<f32, <f32 as Num>::FromStrRadixErr>
sourceimpl Num for usize
impl Num for usize
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<usize, ParseIntError>
sourceimpl Num for f64
impl Num for f64
type FromStrRadixErr = ParseFloatError
fn from_str_radix(
src: &str,
radix: u32
) -> Result<f64, <f64 as Num>::FromStrRadixErr>
sourceimpl<T> Num for Wrapping<T> where
T: Num,
Wrapping<T>: NumOps<Wrapping<T>, Wrapping<T>>,
impl<T> Num for Wrapping<T> where
T: Num,
Wrapping<T>: NumOps<Wrapping<T>, Wrapping<T>>,
type FromStrRadixErr = <T as Num>::FromStrRadixErr
fn from_str_radix(
str: &str,
radix: u32
) -> Result<Wrapping<T>, <Wrapping<T> as Num>::FromStrRadixErr>
sourceimpl Num for u128
impl Num for u128
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<u128, ParseIntError>
sourceimpl Num for u8
impl Num for u8
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<u8, ParseIntError>
sourceimpl Num for u32
impl Num for u32
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<u32, ParseIntError>
sourceimpl Num for u64
impl Num for u64
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<u64, ParseIntError>
sourceimpl Num for i64
impl Num for i64
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<i64, ParseIntError>
sourceimpl Num for isize
impl Num for isize
type FromStrRadixErr = ParseIntError
fn from_str_radix(s: &str, radix: u32) -> Result<isize, ParseIntError>
sourceimpl<T> Num for Complex<T> where
T: Num + Clone,
impl<T> Num for Complex<T> where
T: Num + Clone,
sourcefn from_str_radix(
s: &str,
radix: u32
) -> Result<Complex<T>, <Complex<T> as Num>::FromStrRadixErr>
fn from_str_radix(
s: &str,
radix: u32
) -> Result<Complex<T>, <Complex<T> as Num>::FromStrRadixErr>
Parses a +/- bi
; ai +/- b
; a
; or bi
where a
and b
are of type T
radix
must be <= 18; larger radix would include i and j as digits,
which cannot be supported.
The conversion returns an error if 18 <= radix <= 36; it panics if radix > 36.
The elements of T
are parsed using Num::from_str_radix
too, and errors
(or panics) from that are reflected here as well.
type FromStrRadixErr = ParseComplexError<<T as Num>::FromStrRadixErr>
sourceimpl<T> Num for Ratio<T> where
T: Clone + Integer,
impl<T> Num for Ratio<T> where
T: Clone + Integer,
sourcefn from_str_radix(s: &str, radix: u32) -> Result<Ratio<T>, ParseRatioError>
fn from_str_radix(s: &str, radix: u32) -> Result<Ratio<T>, ParseRatioError>
Parses numer/denom
where the numbers are in base radix
.