num_traits

Trait Num

Source
pub trait Num:
    PartialEq
    + Zero
    + One
    + NumOps {
    type FromStrRadixErr;

    // Required method
    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§

Source

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Num for f32

Source§

type FromStrRadixErr = ParseFloatError

Source§

fn from_str_radix(src: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>

Source§

impl Num for f64

Source§

type FromStrRadixErr = ParseFloatError

Source§

fn from_str_radix(src: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>

Source§

impl Num for i8

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for i16

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for i32

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for i64

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for i128

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for isize

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for u8

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for u16

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for u32

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for u64

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for u128

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl Num for usize

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>

Source§

impl<T: Num> Num for Wrapping<T>
where Wrapping<T>: NumOps,

Source§

type FromStrRadixErr = <T as Num>::FromStrRadixErr

Source§

fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>

Implementors§