# Trait core::convert::From

1.0.0 · source · [−]

    pub trait From<T>: Sized {
        fn from(value: T) -> Self;
    }

Expand description

Used to do value-to-value conversions while consuming the input value. It is the reciprocal of `Into`.

One should always prefer implementing `From` over `Into` because implementing `From` automatically provides one with an implementation of `Into` thanks to the blanket implementation in the standard library.

Only implement `Into` when targeting a version prior to Rust 1.41 and converting to a type outside the current crate. `From` was not able to do these types of conversions in earlier versions because of Rust’s orphaning rules. See `Into` for more details.

Prefer using `Into` over using `From` when specifying trait bounds on a generic function. This way, types that directly implement `Into` can be used as arguments as well.

The `From` is also very useful when performing error handling. When constructing a function that is capable of failing, the return type will generally be of the form `Result<T, E>`. The `From` trait simplifies error handling by allowing a function to return a single error type that encapsulate multiple error types. See the “Examples” section and the book for more details.

Note: This trait must not fail. The `From` trait is intended for perfect conversions. If the conversion can fail or is not perfect, use `TryFrom`.

## Generic Implementations

- `From<T> for U` implies `Into``<U> for T`
- `From` is reflexive, which means that `From<T> for T` is implemented

## Examples

`String` implements `From<&str>`:

An explicit conversion from a `&str` to a String is done as follows:

    let string = "hello".to_string();
    let other_string = String::from("hello");

    assert_eq!(string, other_string);

Run

While performing error handling it is often useful to implement `From` for your own error type. By converting underlying error types to our own custom error type that encapsulates the underlying error type, we can return a single error type without losing information on the underlying cause. The ‘?’ operator automatically converts the underlying error type to our custom error type by calling `Into<CliError>::into` which is automatically provided when implementing `From`. The compiler then infers which implementation of `Into` should be used.

    use std::fs;
    use std::io;
    use std::num;

    enum CliError {
        IoError(io::Error),
        ParseError(num::ParseIntError),
    }

    impl From<io::Error> for CliError {
        fn from(error: io::Error) -> Self {
            CliError::IoError(error)
        }
    }

    impl From<num::ParseIntError> for CliError {
        fn from(error: num::ParseIntError) -> Self {
            CliError::ParseError(error)
        }
    }

    fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
        let mut contents = fs::read_to_string(&file_name)?;
        let num: i32 = contents.trim().parse()?;
        Ok(num)
    }

Run

## Required Methods

source

#### fn from(value: T) -> Self

Converts to this type from the input type.

## Implementors

1.36.0 (const: unstable) · source

### impl From<Infallible> for TryFromSliceError

1.34.0 (const: unstable) · source

### impl From<Infallible> for TryFromIntError

1.28.0 (const: unstable) · source

### impl From<bool> for i8

1.28.0 (const: unstable) · source

### impl From<bool> for i16

1.28.0 (const: unstable) · source

### impl From<bool> for i32

1.28.0 (const: unstable) · source

### impl From<bool> for i64

1.28.0 (const: unstable) · source

### impl From<bool> for i128

1.28.0 (const: unstable) · source

### impl From<bool> for isize

1.28.0 (const: unstable) · source

### impl From<bool> for u8

1.28.0 (const: unstable) · source

### impl From<bool> for u16

1.28.0 (const: unstable) · source

### impl From<bool> for u32

1.28.0 (const: unstable) · source

### impl From<bool> for u64

1.28.0 (const: unstable) · source

### impl From<bool> for u128

1.28.0 (const: unstable) · source

### impl From<bool> for usize

1.24.0 (const: unstable) · source

### impl From<bool> for AtomicBool

1.13.0 (const: unstable) · source

### impl From<char> for u32

1.51.0 (const: unstable) · source

### impl From<char> for u64

1.51.0 (const: unstable) · source

### impl From<char> for u128

1.6.0 (const: unstable) · source

### impl From<f32> for f64

1.6.0 (const: unstable) · source

### impl From<i8> for f32

1.6.0 (const: unstable) · source

### impl From<i8> for f64

1.5.0 (const: unstable) · source

### impl From<i8> for i16

1.5.0 (const: unstable) · source

### impl From<i8> for i32

1.5.0 (const: unstable) · source

### impl From<i8> for i64

1.26.0 (const: unstable) · source

### impl From<i8> for i128

1.5.0 (const: unstable) · source

### impl From<i8> for isize

1.34.0 (const: unstable) · source

### impl From<i8> for AtomicI8

1.6.0 (const: unstable) · source

### impl From<i16> for f32

1.6.0 (const: unstable) · source

### impl From<i16> for f64

1.5.0 (const: unstable) · source

### impl From<i16> for i32

1.5.0 (const: unstable) · source

### impl From<i16> for i64

1.26.0 (const: unstable) · source

### impl From<i16> for i128

1.26.0 (const: unstable) · source

### impl From<i16> for isize

1.34.0 (const: unstable) · source

### impl From<i16> for AtomicI16

1.6.0 (const: unstable) · source

### impl From<i32> for f64

1.5.0 (const: unstable) · source

### impl From<i32> for i64

1.26.0 (const: unstable) · source

### impl From<i32> for i128

1.34.0 (const: unstable) · source

### impl From<i32> for AtomicI32

1.26.0 (const: unstable) · source

### impl From<i64> for i128

1.34.0 (const: unstable) · source

### impl From<i64> for AtomicI64

1.23.0 (const: unstable) · source

### impl From<isize> for AtomicIsize

1.34.0 (const: unstable) · source

### impl From<!> for Infallible

source

### impl From<!> for TryFromIntError

1.13.0 (const: unstable) · source

### impl From<u8> for char

Maps a byte in 0x00..=0xFF to a `char` whose code point has the same value, in U+0000..=U+00FF.

Unicode is designed such that this effectively decodes bytes with the character encoding that IANA calls ISO-8859-1. This encoding is compatible with ASCII.

Note that this is different from ISO/IEC 8859-1 a.k.a. ISO 8859-1 (with one less hyphen), which leaves some “blanks”, byte values that are not assigned to any character. ISO-8859-1 (the IANA one) assigns them to the C0 and C1 control codes.

Note that this is also different from Windows-1252 a.k.a. code page 1252, which is a superset ISO/IEC 8859-1 that assigns some (not all!) blanks to punctuation and various Latin characters.

To confuse things further, on the Web `ascii`, `iso-8859-1`, and `windows-1252` are all aliases for a superset of Windows-1252 that fills the remaining blanks with corresponding C0 and C1 control codes.

1.6.0 (const: unstable) · source

### impl From<u8> for f32

1.6.0 (const: unstable) · source

### impl From<u8> for f64

1.5.0 (const: unstable) · source

### impl From<u8> for i16

1.5.0 (const: unstable) · source

### impl From<u8> for i32

1.5.0 (const: unstable) · source

### impl From<u8> for i64

1.26.0 (const: unstable) · source

### impl From<u8> for i128

1.26.0 (const: unstable) · source

### impl From<u8> for isize

1.5.0 (const: unstable) · source

### impl From<u8> for u16

1.5.0 (const: unstable) · source

### impl From<u8> for u32

1.5.0 (const: unstable) · source

### impl From<u8> for u64

1.26.0 (const: unstable) · source

### impl From<u8> for u128

1.5.0 (const: unstable) · source

### impl From<u8> for usize

1.34.0 (const: unstable) · source

### impl From<u8> for AtomicU8

1.6.0 (const: unstable) · source

### impl From<u16> for f32

1.6.0 (const: unstable) · source

### impl From<u16> for f64

1.5.0 (const: unstable) · source

### impl From<u16> for i32

1.5.0 (const: unstable) · source

### impl From<u16> for i64

1.26.0 (const: unstable) · source

### impl From<u16> for i128

1.5.0 (const: unstable) · source

### impl From<u16> for u32

1.5.0 (const: unstable) · source

### impl From<u16> for u64

1.26.0 (const: unstable) · source

### impl From<u16> for u128

1.26.0 (const: unstable) · source

### impl From<u16> for usize

1.34.0 (const: unstable) · source

### impl From<u16> for AtomicU16

1.6.0 (const: unstable) · source

### impl From<u32> for f64

1.5.0 (const: unstable) · source

### impl From<u32> for i64

1.26.0 (const: unstable) · source

### impl From<u32> for i128

1.5.0 (const: unstable) · source

### impl From<u32> for u64

1.26.0 (const: unstable) · source

### impl From<u32> for u128

1.34.0 (const: unstable) · source

### impl From<u32> for AtomicU32

1.26.0 (const: unstable) · source

### impl From<u64> for i128

1.26.0 (const: unstable) · source

### impl From<u64> for u128

1.34.0 (const: unstable) · source

### impl From<u64> for AtomicU64

1.23.0 (const: unstable) · source

### impl From<usize> for AtomicUsize

source

### impl From<__m128> for f32x4

source

### impl From<__m128d> for f64x2

source

### impl From<__m128i> for i8x16

source

### impl From<__m128i> for i16x8

source

### impl From<__m128i> for i32x4

source

### impl From<__m128i> for i64x2

source

### impl From<__m128i> for isizex2

source

### impl From<__m128i> for u8x16

source

### impl From<__m128i> for u16x8

source

### impl From<__m128i> for u32x4

source

### impl From<__m128i> for u64x2

source

### impl From<__m128i> for usizex2

source

### impl From<__m256> for f32x8

source

### impl From<__m256d> for f64x4

source

### impl From<__m256i> for i8x32

source

### impl From<__m256i> for i16x16

source

### impl From<__m256i> for i32x8

source

### impl From<__m256i> for i64x4

source

### impl From<__m256i> for isizex4

source

### impl From<__m256i> for u8x32

source

### impl From<__m256i> for u16x16

source

### impl From<__m256i> for u32x8

source

### impl From<__m256i> for u64x4

source

### impl From<__m256i> for usizex4

source

### impl From<__m512> for f32x16

source

### impl From<__m512d> for f64x8

source

### impl From<__m512i> for i8x64

source

### impl From<__m512i> for i16x32

source

### impl From<__m512i> for i32x16

source

### impl From<__m512i> for i64x8

source

### impl From<__m512i> for isizex8

source

### impl From<__m512i> for u8x64

source

### impl From<__m512i> for u16x32

source

### impl From<__m512i> for u32x16

source

### impl From<__m512i> for u64x8

source

### impl From<__m512i> for usizex8

1.31.0 (const: unstable) · source

### impl From<NonZeroI8> for i8

1.41.0 (const: unstable) · source

### impl From<NonZeroI8> for NonZeroI16

1.41.0 (const: unstable) · source

### impl From<NonZeroI8> for NonZeroI32

1.41.0 (const: unstable) · source

### impl From<NonZeroI8> for NonZeroI64

1.41.0 (const: unstable) · source

### impl From<NonZeroI8> for NonZeroI128

1.41.0 (const: unstable) · source

### impl From<NonZeroI8> for NonZeroIsize

1.31.0 (const: unstable) · source

### impl From<NonZeroI16> for i16

1.41.0 (const: unstable) · source

### impl From<NonZeroI16> for NonZeroI32

1.41.0 (const: unstable) · source

### impl From<NonZeroI16> for NonZeroI64

1.41.0 (const: unstable) · source

### impl From<NonZeroI16> for NonZeroI128

1.41.0 (const: unstable) · source

### impl From<NonZeroI16> for NonZeroIsize

1.31.0 (const: unstable) · source

### impl From<NonZeroI32> for i32

1.41.0 (const: unstable) · source

### impl From<NonZeroI32> for NonZeroI64

1.41.0 (const: unstable) · source

### impl From<NonZeroI32> for NonZeroI128

1.31.0 (const: unstable) · source

### impl From<NonZeroI64> for i64

1.41.0 (const: unstable) · source

### impl From<NonZeroI64> for NonZeroI128

1.31.0 (const: unstable) · source

### impl From<NonZeroI128> for i128

1.31.0 (const: unstable) · source

### impl From<NonZeroIsize> for isize

1.31.0 (const: unstable) · source

### impl From<NonZeroU8> for u8

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroI16

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroI32

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroI64

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroI128

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroIsize

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroU16

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroU32

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroU64

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroU128

1.41.0 (const: unstable) · source

### impl From<NonZeroU8> for NonZeroUsize

1.31.0 (const: unstable) · source

### impl From<NonZeroU16> for u16

1.41.0 (const: unstable) · source

### impl From<NonZeroU16> for NonZeroI32

1.41.0 (const: unstable) · source

### impl From<NonZeroU16> for NonZeroI64

1.41.0 (const: unstable) · source

### impl From<NonZeroU16> for NonZeroI128

1.41.0 (const: unstable) · source

### impl From<NonZeroU16> for NonZeroU32

1.41.0 (const: unstable) · source

### impl From<NonZeroU16> for NonZeroU64

1.41.0 (const: unstable) · source

### impl From<NonZeroU16> for NonZeroU128

1.41.0 (const: unstable) · source

### impl From<NonZeroU16> for NonZeroUsize

1.31.0 (const: unstable) · source

### impl From<NonZeroU32> for u32

1.41.0 (const: unstable) · source

### impl From<NonZeroU32> for NonZeroI64

1.41.0 (const: unstable) · source

### impl From<NonZeroU32> for NonZeroI128

1.41.0 (const: unstable) · source

### impl From<NonZeroU32> for NonZeroU64

1.41.0 (const: unstable) · source

### impl From<NonZeroU32> for NonZeroU128

1.31.0 (const: unstable) · source

### impl From<NonZeroU64> for u64

1.41.0 (const: unstable) · source

### impl From<NonZeroU64> for NonZeroI128

1.41.0 (const: unstable) · source

### impl From<NonZeroU64> for NonZeroU128

1.31.0 (const: unstable) · source

### impl From<NonZeroU128> for u128

1.31.0 (const: unstable) · source

### impl From<NonZeroUsize> for usize

source

### impl From<Alignment> for usize

source

### impl From<Alignment> for NonZeroUsize

source

### impl From<Simd<f32, 4>> for __m128

source

### impl From<Simd<f32, 8>> for __m256

source

### impl From<Simd<f32, 16>> for __m512

source

### impl From<Simd<f64, 2>> for __m128d

source

### impl From<Simd<f64, 4>> for __m256d

source

### impl From<Simd<f64, 8>> for __m512d

source

### impl From<Simd<i8, 16>> for __m128i

source

### impl From<Simd<i8, 32>> for __m256i

source

### impl From<Simd<i8, 64>> for __m512i

source

### impl From<Simd<i16, 8>> for __m128i

source

### impl From<Simd<i16, 16>> for __m256i

source

### impl From<Simd<i16, 32>> for __m512i

source

### impl From<Simd<i32, 4>> for __m128i

source

### impl From<Simd<i32, 8>> for __m256i

source

### impl From<Simd<i32, 16>> for __m512i

source

### impl From<Simd<i64, 2>> for __m128i

source

### impl From<Simd<i64, 4>> for __m256i

source

### impl From<Simd<i64, 8>> for __m512i

source

### impl From<Simd<isize, 2>> for __m128i

source

### impl From<Simd<isize, 4>> for __m256i

source

### impl From<Simd<isize, 8>> for __m512i

source

### impl From<Simd<u8, 16>> for __m128i

source

### impl From<Simd<u8, 32>> for __m256i

source

### impl From<Simd<u8, 64>> for __m512i

source

### impl From<Simd<u16, 8>> for __m128i

source

### impl From<Simd<u16, 16>> for __m256i

source

### impl From<Simd<u16, 32>> for __m512i

source

### impl From<Simd<u32, 4>> for __m128i

source

### impl From<Simd<u32, 8>> for __m256i

source

### impl From<Simd<u32, 16>> for __m512i

source

### impl From<Simd<u64, 2>> for __m128i

source

### impl From<Simd<u64, 4>> for __m256i

source

### impl From<Simd<u64, 8>> for __m512i

source

### impl From<Simd<usize, 2>> for __m128i

source

### impl From<Simd<usize, 4>> for __m256i

source

### impl From<Simd<usize, 8>> for __m512i

1.30.0 (const: unstable) · source

### impl<'a, T> From<&'a Option<T>> for Option<&'a T>

1.30.0 (const: unstable) · source

### impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>

1.34.0 (const: unstable) · source

### impl<T> From<!> for T

Stability note: This impl does not yet exist, but we are “reserving space” to add it in the future. See rust-lang/rust#64715 for details.

1.23.0 (const: unstable) · source

### impl<T> From<*mut T> for AtomicPtr<T>

1.12.0 (const: unstable) · source

### impl<T> From<T> for Option<T>

1.36.0 (const: unstable) · source

### impl<T> From<T> for Poll<T>

1.12.0 (const: unstable) · source

### impl<T> From<T> for Cell<T>

source

### impl<T> From<T> for OnceCell<T>

1.12.0 (const: unstable) · source

### impl<T> From<T> for RefCell<T>

const: unstable · source

### impl<T> From<T> for SyncUnsafeCell<T>

1.12.0 (const: unstable) · source

### impl<T> From<T> for UnsafeCell<T>

source

### impl<T> From<T> for Exclusive<T>

const: unstable · source

### impl<T> From<T> for T

source

### impl<T, const LANES: usize> From<[T; LANES]> for Simd<T, LANES>where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,

source

### impl<T, const LANES: usize> From<Mask<T, LANES>> for [bool; LANES]where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,

source

### impl<T, const LANES: usize> From<Simd<T, LANES>> for [T; LANES]where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,

source

### impl<T, const LANES: usize> From<[bool; LANES]> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,

1.25.0 (const: unstable) · source

### impl<T: ?Sized> From<&T> for NonNull<T>

1.25.0 (const: unstable) · source

### impl<T: ?Sized> From<&mut T> for NonNull<T>

source

### impl<const LANES: usize> From<Mask<i8, LANES>> for Mask<i16, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i8, LANES>> for Mask<i32, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i8, LANES>> for Mask<i64, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i8, LANES>> for Mask<isize, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i16, LANES>> for Mask<i8, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i16, LANES>> for Mask<i32, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i16, LANES>> for Mask<i64, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i16, LANES>> for Mask<isize, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i32, LANES>> for Mask<i8, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i32, LANES>> for Mask<i16, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i32, LANES>> for Mask<i64, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i32, LANES>> for Mask<isize, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i64, LANES>> for Mask<i8, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i64, LANES>> for Mask<i16, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i64, LANES>> for Mask<i32, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<i64, LANES>> for Mask<isize, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<isize, LANES>> for Mask<i8, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<isize, LANES>> for Mask<i16, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<isize, LANES>> for Mask<i32, LANES>where
LaneCount<LANES>: SupportedLaneCount,

source

### impl<const LANES: usize> From<Mask<isize, LANES>> for Mask<i64, LANES>where
LaneCount<LANES>: SupportedLaneCount,