NumCast in num_traits::cast - Rust
Description: An interface for casting between machine scalars.
Keywords: rust, rustlang, rust-lang, NumCast

# Trait num_traits::cast::NumCast

source ·

    pub trait NumCast: Sized + ToPrimitive {
        fn from<T: ToPrimitive>(n: T) -> Option<Self>;
    }

Expand description

An interface for casting between machine scalars.

## Required Methods§

source

#### fn from<T: ToPrimitive>(n: T) -> Option<Self>

Creates a number from another value that can be converted into a primitive via the `ToPrimitive` trait. If the source value cannot be represented by the target type, then `None` is returned.

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.

## Implementations on Foreign Types§

source§

### impl NumCast for u8

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<u8>

source§

### impl NumCast for u16

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<u16>

source§

### impl NumCast for u32

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<u32>

source§

### impl NumCast for u64

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<u64>

source§

### impl NumCast for u128

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<u128>

source§

### impl NumCast for usize

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<usize>

source§

### impl NumCast for i8

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<i8>

source§

### impl NumCast for i16

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<i16>

source§

### impl NumCast for i32

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<i32>

source§

### impl NumCast for i64

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<i64>

source§

### impl NumCast for i128

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<i128>

source§

### impl NumCast for isize

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<isize>

source§

### impl NumCast for f32

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<f32>

source§

### impl NumCast for f64

source§

#### fn from<N: ToPrimitive>(n: N) -> Option<f64>

source§

### impl<T: NumCast> NumCast for Wrapping<T>

source§

#### fn from<U: ToPrimitive>(n: U) -> Option<Self>

## Implementors§