core::convert

Trait From

1.6.0 · Source
pub trait From<T>: Sized {
    // Required method
    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 trait 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>. From simplifies error handling by allowing a function to return a single error type that encapsulates 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

§When to implement From

While there’s no technical restrictions on which conversions can be done using a From implementation, the general expectation is that the conversions should typically be restricted as follows:

  • The conversion is infallible: if the conversion can fail, use TryFrom instead; don’t provide a From impl that panics.

  • The conversion is lossless: semantically, it should not lose or discard information. For example, i32: From<u16> exists, where the original value can be recovered using u16: TryFrom<i32>. And String: From<&str> exists, where you can get something equivalent to the original value via Deref. But From cannot be used to convert from u32 to u16, since that cannot succeed in a lossless way. (There’s some wiggle room here for information not considered semantically relevant. For example, Box<[T]>: From<Vec<T>> exists even though it might not preserve capacity, like how two vectors can be equal despite differing capacities.)

  • The conversion is value-preserving: the conceptual kind and meaning of the resulting value is the same, even though the Rust type and technical representation might be different. For example -1_i8 as u8 is lossless, since as casting back can recover the original value, but that conversion is not available via From because -1 and 255 are different conceptual values (despite being identical bit patterns technically). But f32: From<i16> is available because 1_i16 and 1.0_f32 are conceptually the same real number (despite having very different bit patterns technically). String: From<char> is available because they’re both text, but String: From<u32> is not available, since 1 (a number) and "1" (text) are too different. (Converting values to text is instead covered by the Display trait.)

  • The conversion is obvious: it’s the only reasonable conversion between the two types. Otherwise it’s better to have it be a named method or constructor, like how str::as_bytes is a method and how integers have methods like u32::from_ne_bytes, u32::from_le_bytes, and u32::from_be_bytes, none of which are From implementations. Whereas there’s only one reasonable way to wrap an Ipv6Addr into an IpAddr, thus IpAddr: From<Ipv6Addr> exists.

§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);

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 with From::from.

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)
}

Required Methods§

1.0.0 · Source

fn from(value: T) -> Self

Converts to this type from the input 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.

Implementors§

Source§

impl From<AsciiChar> for char

Source§

impl From<AsciiChar> for u8

Source§

impl From<AsciiChar> for u16

Source§

impl From<AsciiChar> for u32

Source§

impl From<AsciiChar> for u64

Source§

impl From<AsciiChar> for u128

1.36.0 · Source§

impl From<Infallible> for TryFromSliceError

1.34.0 · Source§

impl From<Infallible> for TryFromIntError

1.68.0 · Source§

impl From<bool> for f32

1.68.0 · Source§

impl From<bool> for f64

1.28.0 · Source§

impl From<bool> for i8

1.28.0 · Source§

impl From<bool> for i16

1.28.0 · Source§

impl From<bool> for i32

1.28.0 · Source§

impl From<bool> for i64

1.28.0 · Source§

impl From<bool> for i128

1.28.0 · Source§

impl From<bool> for isize

1.28.0 · Source§

impl From<bool> for u8

1.28.0 · Source§

impl From<bool> for u16

1.28.0 · Source§

impl From<bool> for u32

1.28.0 · Source§

impl From<bool> for u64

1.28.0 · Source§

impl From<bool> for u128

1.28.0 · Source§

impl From<bool> for usize

1.24.0 · Source§

impl From<bool> for AtomicBool

1.13.0 · Source§

impl From<char> for u32

1.51.0 · Source§

impl From<char> for u64

1.51.0 · Source§

impl From<char> for u128

1.6.0 · Source§

impl From<f16> for f64

1.6.0 · Source§

impl From<f16> for f128

1.6.0 · Source§

impl From<f32> for f64

1.6.0 · Source§

impl From<f32> for f128

1.6.0 · Source§

impl From<f64> for f128

1.6.0 · Source§

impl From<i8> for f32

1.6.0 · Source§

impl From<i8> for f64

1.5.0 · Source§

impl From<i8> for i16

1.5.0 · Source§

impl From<i8> for i32

1.5.0 · Source§

impl From<i8> for i64

1.26.0 · Source§

impl From<i8> for i128

1.5.0 · Source§

impl From<i8> for isize

1.34.0 · Source§

impl From<i8> for AtomicI8

1.6.0 · Source§

impl From<i16> for f32

1.6.0 · Source§

impl From<i16> for f64

1.5.0 · Source§

impl From<i16> for i32

1.5.0 · Source§

impl From<i16> for i64

1.26.0 · Source§

impl From<i16> for i128

1.26.0 · Source§

impl From<i16> for isize

1.34.0 · Source§

impl From<i16> for AtomicI16

1.6.0 · Source§

impl From<i32> for f64

1.5.0 · Source§

impl From<i32> for i64

1.26.0 · Source§

impl From<i32> for i128

1.34.0 · Source§

impl From<i32> for AtomicI32

1.26.0 · Source§

impl From<i64> for i128

1.34.0 · Source§

impl From<i64> for AtomicI64

1.23.0 · Source§

impl From<isize> for AtomicIsize

1.34.0 · Source§

impl From<!> for Infallible

Source§

impl From<!> for TryFromIntError

1.13.0 · 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 · Source§

impl From<u8> for f32

1.6.0 · Source§

impl From<u8> for f64

1.5.0 · Source§

impl From<u8> for i16

1.5.0 · Source§

impl From<u8> for i32

1.5.0 · Source§

impl From<u8> for i64

1.26.0 · Source§

impl From<u8> for i128

1.26.0 · Source§

impl From<u8> for isize

1.5.0 · Source§

impl From<u8> for u16

1.5.0 · Source§

impl From<u8> for u32

1.5.0 · Source§

impl From<u8> for u64

1.26.0 · Source§

impl From<u8> for u128

1.5.0 · Source§

impl From<u8> for usize

1.34.0 · Source§

impl From<u8> for AtomicU8

1.6.0 · Source§

impl From<u16> for f32

1.6.0 · Source§

impl From<u16> for f64

1.5.0 · Source§

impl From<u16> for i32

1.5.0 · Source§

impl From<u16> for i64

1.26.0 · Source§

impl From<u16> for i128

1.5.0 · Source§

impl From<u16> for u32

1.5.0 · Source§

impl From<u16> for u64

1.26.0 · Source§

impl From<u16> for u128

1.26.0 · Source§

impl From<u16> for usize

1.34.0 · Source§

impl From<u16> for AtomicU16

1.6.0 · Source§

impl From<u32> for f64

1.5.0 · Source§

impl From<u32> for i64

1.26.0 · Source§

impl From<u32> for i128

1.5.0 · Source§

impl From<u32> for u64

1.26.0 · Source§

impl From<u32> for u128

1.1.0 · Source§

impl From<u32> for Ipv4Addr

1.34.0 · Source§

impl From<u32> for AtomicU32

1.26.0 · Source§

impl From<u64> for i128

1.26.0 · Source§

impl From<u64> for u128

1.34.0 · Source§

impl From<u64> for AtomicU64

1.26.0 · Source§

impl From<u128> for Ipv6Addr

1.23.0 · Source§

impl From<usize> for AtomicUsize

Source§

impl From<v128> for f32x4

Source§

impl From<v128> for f64x2

Source§

impl From<v128> for i8x16

Source§

impl From<v128> for i16x8

Source§

impl From<v128> for i32x4

Source§

impl From<v128> for i64x2

Source§

impl From<v128> for isizex4

Source§

impl From<v128> for u8x16

Source§

impl From<v128> for u16x8

Source§

impl From<v128> for u32x4

Source§

impl From<v128> for u64x2

Source§

impl From<v128> for usizex4

1.16.0 · Source§

impl From<Ipv4Addr> for IpAddr

1.1.0 · Source§

impl From<Ipv4Addr> for u32

1.16.0 · Source§

impl From<Ipv6Addr> for IpAddr

1.26.0 · Source§

impl From<Ipv6Addr> for u128

1.16.0 · Source§

impl From<SocketAddrV4> for SocketAddr

1.16.0 · Source§

impl From<SocketAddrV6> for SocketAddr

1.41.0 · Source§

impl From<NonZero<i8>> for NonZero<i16>

1.41.0 · Source§

impl From<NonZero<i8>> for NonZero<i32>

1.41.0 · Source§

impl From<NonZero<i8>> for NonZero<i64>

1.41.0 · Source§

impl From<NonZero<i8>> for NonZero<i128>

1.41.0 · Source§

impl From<NonZero<i8>> for NonZero<isize>

1.41.0 · Source§

impl From<NonZero<i16>> for NonZero<i32>

1.41.0 · Source§

impl From<NonZero<i16>> for NonZero<i64>

1.41.0 · Source§

impl From<NonZero<i16>> for NonZero<i128>

1.41.0 · Source§

impl From<NonZero<i16>> for NonZero<isize>

1.41.0 · Source§

impl From<NonZero<i32>> for NonZero<i64>

1.41.0 · Source§

impl From<NonZero<i32>> for NonZero<i128>

1.41.0 · Source§

impl From<NonZero<i64>> for NonZero<i128>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<i16>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<i32>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<i64>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<i128>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<isize>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<u16>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<u32>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<u64>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<u128>

1.41.0 · Source§

impl From<NonZero<u8>> for NonZero<usize>

1.41.0 · Source§

impl From<NonZero<u16>> for NonZero<i32>

1.41.0 · Source§

impl From<NonZero<u16>> for NonZero<i64>

1.41.0 · Source§

impl From<NonZero<u16>> for NonZero<i128>

1.41.0 · Source§

impl From<NonZero<u16>> for NonZero<u32>

1.41.0 · Source§

impl From<NonZero<u16>> for NonZero<u64>

1.41.0 · Source§

impl From<NonZero<u16>> for NonZero<u128>

1.41.0 · Source§

impl From<NonZero<u16>> for NonZero<usize>

1.41.0 · Source§

impl From<NonZero<u32>> for NonZero<i64>

1.41.0 · Source§

impl From<NonZero<u32>> for NonZero<i128>

1.41.0 · Source§

impl From<NonZero<u32>> for NonZero<u64>

1.41.0 · Source§

impl From<NonZero<u32>> for NonZero<u128>

1.41.0 · Source§

impl From<NonZero<u64>> for NonZero<i128>

1.41.0 · Source§

impl From<NonZero<u64>> for NonZero<u128>

Source§

impl From<Alignment> for usize

Source§

impl From<Alignment> for NonZero<usize>

Source§

impl From<Simd<f32, 4>> for v128

Source§

impl From<Simd<f64, 2>> for v128

Source§

impl From<Simd<i8, 16>> for v128

Source§

impl From<Simd<i16, 8>> for v128

Source§

impl From<Simd<i32, 4>> for v128

Source§

impl From<Simd<i64, 2>> for v128

Source§

impl From<Simd<isize, 4>> for v128

Source§

impl From<Simd<u8, 16>> for v128

Source§

impl From<Simd<u16, 8>> for v128

Source§

impl From<Simd<u32, 4>> for v128

Source§

impl From<Simd<u64, 2>> for v128

Source§

impl From<Simd<usize, 4>> for v128

1.17.0 · Source§

impl From<[u8; 4]> for IpAddr

1.9.0 · Source§

impl From<[u8; 4]> for Ipv4Addr

1.17.0 · Source§

impl From<[u8; 16]> for IpAddr

1.9.0 · Source§

impl From<[u8; 16]> for Ipv6Addr

1.17.0 · Source§

impl From<[u16; 8]> for IpAddr

1.16.0 · Source§

impl From<[u16; 8]> for Ipv6Addr

1.30.0 · Source§

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

1.30.0 · Source§

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

Source§

impl<'data> From<&'data mut [u8]> for BorrowedBuf<'data>

Creates a new BorrowedBuf from a fully initialized slice.

Source§

impl<'data> From<&'data mut [MaybeUninit<u8>]> for BorrowedBuf<'data>

Creates a new BorrowedBuf from an uninitialized buffer.

Use set_init if part of the buffer is known to be already initialized.

1.17.0 · Source§

impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr

1.71.0 · Source§

impl<T> From<[T; N]> for (T₁, T₂, …, Tₙ)

This trait is implemented for tuples up to twelve items long.

1.34.0 · 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 · Source§

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

1.71.0 · Source§

impl<T> From<(T₁, T₂, …, Tₙ)> for [T; N]

This trait is implemented for tuples up to twelve items long.

1.31.0 · Source§

impl<T> From<NonZero<T>> for T

Source§

impl<T> From<Range<T>> for core::range::Range<T>

Source§

impl<T> From<RangeFrom<T>> for core::range::RangeFrom<T>

Source§

impl<T> From<RangeInclusive<T>> for core::range::RangeInclusive<T>

Source§

impl<T> From<Range<T>> for core::ops::Range<T>

Source§

impl<T> From<RangeFrom<T>> for core::ops::RangeFrom<T>

Source§

impl<T> From<RangeInclusive<T>> for core::ops::RangeInclusive<T>

1.12.0 · Source§

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

1.36.0 · Source§

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

1.12.0 · Source§

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

1.70.0 · Source§

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

1.12.0 · Source§

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

Source§

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

1.12.0 · Source§

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

Source§

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

1.0.0 · Source§

impl<T> From<T> for T

Source§

impl<T, const N: usize> From<[T; N]> for Simd<T, N>

Source§

impl<T, const N: usize> From<Mask<T, N>> for [bool; N]

Source§

impl<T, const N: usize> From<Simd<T, N>> for [T; N]

Source§

impl<T, const N: usize> From<[bool; N]> for Mask<T, N>

1.25.0 · Source§

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

1.25.0 · Source§

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

Source§

impl<const N: usize> From<Mask<i8, N>> for Mask<i16, N>

Source§

impl<const N: usize> From<Mask<i8, N>> for Mask<i32, N>

Source§

impl<const N: usize> From<Mask<i8, N>> for Mask<i64, N>

Source§

impl<const N: usize> From<Mask<i8, N>> for Mask<isize, N>

Source§

impl<const N: usize> From<Mask<i16, N>> for Mask<i8, N>

Source§

impl<const N: usize> From<Mask<i16, N>> for Mask<i32, N>

Source§

impl<const N: usize> From<Mask<i16, N>> for Mask<i64, N>

Source§

impl<const N: usize> From<Mask<i16, N>> for Mask<isize, N>

Source§

impl<const N: usize> From<Mask<i32, N>> for Mask<i8, N>

Source§

impl<const N: usize> From<Mask<i32, N>> for Mask<i16, N>

Source§

impl<const N: usize> From<Mask<i32, N>> for Mask<i64, N>

Source§

impl<const N: usize> From<Mask<i32, N>> for Mask<isize, N>

Source§

impl<const N: usize> From<Mask<i64, N>> for Mask<i8, N>

Source§

impl<const N: usize> From<Mask<i64, N>> for Mask<i16, N>

Source§

impl<const N: usize> From<Mask<i64, N>> for Mask<i32, N>

Source§

impl<const N: usize> From<Mask<i64, N>> for Mask<isize, N>

Source§

impl<const N: usize> From<Mask<isize, N>> for Mask<i8, N>

Source§

impl<const N: usize> From<Mask<isize, N>> for Mask<i16, N>

Source§

impl<const N: usize> From<Mask<isize, N>> for Mask<i32, N>

Source§

impl<const N: usize> From<Mask<isize, N>> for Mask<i64, N>