pub trait From<T>:Sized { // Required method fnfrom(value: T) -> Self;}Expand description
Used to do value-to-value conversions while consuming the input value. It is the reciprocal ofInto.
One should always prefer implementingFrom overIntobecause implementingFrom automatically provides one with an implementation ofIntothanks to the blanket implementation in the standard library.
Only implementInto when targeting a version prior to Rust 1.41 and converting to a typeoutside the current crate.From was not able to do these types of conversions in earlier versions because of Rust’sorphaning rules.SeeInto for more details.
Prefer usingInto overFrom when specifying trait bounds on a generic functionto ensure that types that only implementInto can be used as well.
TheFrom trait is also very useful when performing error handling. When constructing a functionthat is capable of failing, the return type will generally be of the formResult<T, E>.From simplifies error handling by allowing a function to return a single error typethat encapsulates multiple error types. See the “Examples” section andthe book for moredetails.
Note: This trait must not fail. TheFrom trait is intended for perfect conversions.If the conversion can fail or is not perfect, useTryFrom.
§Generic Implementations
From<T> for UimpliesInto<U> for TFromis reflexive, which means thatFrom<T> for Tis implemented
§When to implementFrom
While there’s no technical restrictions on which conversions can be done usingaFrom implementation, the general expectation is that the conversionsshould typically be restricted as follows:
The conversion isinfallible: if the conversion can fail, use
TryFrominstead; don’t provide aFromimpl that panics.The conversion islossless: semantically, it should not lose or discardinformation. For example,
i32: From<u16>exists, where the originalvalue can be recovered usingu16: TryFrom<i32>. AndString: From<&str>exists, where you can get something equivalent to the original value viaDeref. ButFromcannot be used to convert fromu32tou16, sincethat cannot succeed in a lossless way. (There’s some wiggle room here forinformation 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 isvalue-preserving: the conceptual kind and meaning ofthe resulting value is the same, even though the Rust type and technicalrepresentation might be different. For example
-1_i8 as u8islossless,sinceascasting back can recover the original value, but that conversionisnot available viaFrombecause-1and255are different conceptualvalues (despite being identical bit patterns technically). Butf32: From<i16>is available because1_i16and1.0_f32are conceptuallythe same real number (despite having very different bit patterns technically).String: From<char>is available because they’re bothtext, butString: From<u32>isnot available, since1(a number) and"1"(text) are too different. (Converting values to text is instead coveredby theDisplaytrait.)The conversion isobvious: it’s the only reasonable conversion betweenthe two types. Otherwise it’s better to have it be a named method orconstructor, like how
str::as_bytesis a method and how integers havemethods likeu32::from_ne_bytes,u32::from_le_bytes, andu32::from_be_bytes, none of which areFromimplementations. Whereasthere’s only one reasonable way to wrap anIpv6Addrinto anIpAddr, thusIpAddr: From<Ipv6Addr>exists.
§Examples
String implementsFrom<&str>:
An explicit conversion from a&str to a String is done as follows:
letstring ="hello".to_string();letother_string = String::from("hello");assert_eq!(string, other_string);While performing error handling it is often useful to implementFrom for your own error type.By converting underlying error types to our own custom error type that encapsulates theunderlying error type, we can return a single error type without losing information on theunderlying cause. The ‘?’ operator automatically converts the underlying error type to ourcustom error type withFrom::from.
usestd::fs;usestd::io;usestd::num;enumCliError { IoError(io::Error), ParseError(num::ParseIntError),}implFrom<io::Error>forCliError {fnfrom(error: io::Error) ->Self{ CliError::IoError(error) }}implFrom<num::ParseIntError>forCliError {fnfrom(error: num::ParseIntError) ->Self{ CliError::ParseError(error) }}fnopen_and_parse_file(file_name:&str) ->Result<i32, CliError> {letmutcontents = fs::read_to_string(&file_name)?;letnum: i32 = contents.trim().parse()?;Ok(num)}Required Methods§
Dyn Compatibility§
This trait isnotdyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
implFrom<AsciiChar> forchar
implFrom<AsciiChar> foru8
implFrom<AsciiChar> foru16
implFrom<AsciiChar> foru32
implFrom<AsciiChar> foru64
implFrom<AsciiChar> foru128
implFrom<Infallible> forTryFromSliceError
implFrom<Infallible> forTryFromIntError
implFrom<bool> forf16
implFrom<bool> forf32
implFrom<bool> forf64
implFrom<bool> forf128
implFrom<bool> fori8
implFrom<bool> fori16
implFrom<bool> fori32
implFrom<bool> fori64
implFrom<bool> fori128
implFrom<bool> forisize
implFrom<bool> foru8
implFrom<bool> foru16
implFrom<bool> foru32
implFrom<bool> foru64
implFrom<bool> foru128
implFrom<bool> forusize
implFrom<bool> forAtomicBool
implFrom<char> foru32
implFrom<char> foru64
implFrom<char> foru128
implFrom<f16> forf64
implFrom<f16> forf128
implFrom<f32> forf64
implFrom<f32> forf128
implFrom<f64> forf128
implFrom<i8> forf16
implFrom<i8> forf32
implFrom<i8> forf64
implFrom<i8> forf128
implFrom<i8> fori16
implFrom<i8> fori32
implFrom<i8> fori64
implFrom<i8> fori128
implFrom<i8> forisize
implFrom<i8> forAtomicI8
implFrom<i16> forf32
implFrom<i16> forf64
implFrom<i16> forf128
implFrom<i16> fori32
implFrom<i16> fori64
implFrom<i16> fori128
implFrom<i16> forisize
implFrom<i16> forAtomicI16
implFrom<i32> forf64
implFrom<i32> forf128
implFrom<i32> fori64
implFrom<i32> fori128
implFrom<i32> forAtomicI32
implFrom<i64> fori128
implFrom<i64> forAtomicI64
implFrom<isize> forAtomicIsize
implFrom<!> forInfallible
implFrom<!> forTryFromIntError
implFrom<u8> forchar
Maps a byte in 0x00..=0xFF to achar whose code point has the same value, in U+0000..=U+00FF.
Unicode is designed such that this effectively decodes byteswith 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 isalso different from Windows-1252 a.k.a. code page 1252,which is a superset ISO/IEC 8859-1 that assigns some (not all!) blanksto punctuation and various Latin characters.
To confuse things further,on the Webascii,iso-8859-1, andwindows-1252 are all aliasesfor a superset of Windows-1252 that fills the remaining blanks with correspondingC0 and C1 control codes.
implFrom<u8> forf16
implFrom<u8> forf32
implFrom<u8> forf64
implFrom<u8> forf128
implFrom<u8> fori16
implFrom<u8> fori32
implFrom<u8> fori64
implFrom<u8> fori128
implFrom<u8> forisize
implFrom<u8> foru16
implFrom<u8> foru32
implFrom<u8> foru64
implFrom<u8> foru128
implFrom<u8> forusize
implFrom<u8> forAtomicU8
implFrom<u16> forf32
implFrom<u16> forf64
implFrom<u16> forf128
implFrom<u16> fori32
implFrom<u16> fori64
implFrom<u16> fori128
implFrom<u16> foru32
implFrom<u16> foru64
implFrom<u16> foru128
implFrom<u16> forusize
implFrom<u16> forAtomicU16
implFrom<u32> forf64
implFrom<u32> forf128
implFrom<u32> fori64
implFrom<u32> fori128
implFrom<u32> foru64
implFrom<u32> foru128
implFrom<u32> forIpv4Addr
implFrom<u32> forAtomicU32
implFrom<u64> fori128
implFrom<u64> foru128
implFrom<u64> forAtomicU64
implFrom<u128> forIpv6Addr
implFrom<usize> forAtomicUsize
implFrom<__m128> forf32x4
implFrom<__m128d> forf64x2
implFrom<__m128i> fori8x16
implFrom<__m128i> fori16x8
implFrom<__m128i> fori32x4
implFrom<__m128i> fori64x2
implFrom<__m128i> forisizex2
implFrom<__m128i> foru8x16
implFrom<__m128i> foru16x8
implFrom<__m128i> foru32x4
implFrom<__m128i> foru64x2
implFrom<__m128i> forusizex2
implFrom<__m256> forf32x8
implFrom<__m256d> forf64x4
implFrom<__m256i> fori8x32
implFrom<__m256i> fori16x16
implFrom<__m256i> fori32x8
implFrom<__m256i> fori64x4
implFrom<__m256i> forisizex4
implFrom<__m256i> foru8x32
implFrom<__m256i> foru16x16
implFrom<__m256i> foru32x8
implFrom<__m256i> foru64x4
implFrom<__m256i> forusizex4
implFrom<__m512> forf32x16
implFrom<__m512d> forf64x8
implFrom<__m512i> fori8x64
implFrom<__m512i> fori16x32
implFrom<__m512i> fori32x16
implFrom<__m512i> fori64x8
implFrom<__m512i> forisizex8
implFrom<__m512i> foru8x64
implFrom<__m512i> foru16x32
implFrom<__m512i> foru32x16
implFrom<__m512i> foru64x8
implFrom<__m512i> forusizex8
implFrom<Ipv4Addr> forIpAddr
implFrom<Ipv4Addr> foru32
implFrom<Ipv6Addr> forIpAddr
implFrom<Ipv6Addr> foru128
implFrom<SocketAddrV4> forSocketAddr
implFrom<SocketAddrV6> forSocketAddr
implFrom<NonZero<i8>> forNonZero<i16>
implFrom<NonZero<i8>> forNonZero<i32>
implFrom<NonZero<i8>> forNonZero<i64>
implFrom<NonZero<i8>> forNonZero<i128>
implFrom<NonZero<i8>> forNonZero<isize>
implFrom<NonZero<i16>> forNonZero<i32>
implFrom<NonZero<i16>> forNonZero<i64>
implFrom<NonZero<i16>> forNonZero<i128>
implFrom<NonZero<i16>> forNonZero<isize>
implFrom<NonZero<i32>> forNonZero<i64>
implFrom<NonZero<i32>> forNonZero<i128>
implFrom<NonZero<i64>> forNonZero<i128>
implFrom<NonZero<u8>> forNonZero<i16>
implFrom<NonZero<u8>> forNonZero<i32>
implFrom<NonZero<u8>> forNonZero<i64>
implFrom<NonZero<u8>> forNonZero<i128>
implFrom<NonZero<u8>> forNonZero<isize>
implFrom<NonZero<u8>> forNonZero<u16>
implFrom<NonZero<u8>> forNonZero<u32>
implFrom<NonZero<u8>> forNonZero<u64>
implFrom<NonZero<u8>> forNonZero<u128>
implFrom<NonZero<u8>> forNonZero<usize>
implFrom<NonZero<u16>> forNonZero<i32>
implFrom<NonZero<u16>> forNonZero<i64>
implFrom<NonZero<u16>> forNonZero<i128>
implFrom<NonZero<u16>> forNonZero<u32>
implFrom<NonZero<u16>> forNonZero<u64>
implFrom<NonZero<u16>> forNonZero<u128>
implFrom<NonZero<u16>> forNonZero<usize>
implFrom<NonZero<u32>> forNonZero<i64>
implFrom<NonZero<u32>> forNonZero<i128>
implFrom<NonZero<u32>> forNonZero<u64>
implFrom<NonZero<u32>> forNonZero<u128>
implFrom<NonZero<u64>> forNonZero<i128>
implFrom<NonZero<u64>> forNonZero<u128>
implFrom<Alignment> forusize
implFrom<Alignment> forNonZero<usize>
implFrom<Simd<f32, 4>> for__m128
implFrom<Simd<f32, 8>> for__m256
implFrom<Simd<f32, 16>> for__m512
implFrom<Simd<f64, 2>> for__m128d
implFrom<Simd<f64, 4>> for__m256d
implFrom<Simd<f64, 8>> for__m512d
implFrom<Simd<i8, 16>> for__m128i
implFrom<Simd<i8, 32>> for__m256i
implFrom<Simd<i8, 64>> for__m512i
implFrom<Simd<i16, 8>> for__m128i
implFrom<Simd<i16, 16>> for__m256i
implFrom<Simd<i16, 32>> for__m512i
implFrom<Simd<i32, 4>> for__m128i
implFrom<Simd<i32, 8>> for__m256i
implFrom<Simd<i32, 16>> for__m512i
implFrom<Simd<i64, 2>> for__m128i
implFrom<Simd<i64, 4>> for__m256i
implFrom<Simd<i64, 8>> for__m512i
implFrom<Simd<isize, 2>> for__m128i
implFrom<Simd<isize, 4>> for__m256i
implFrom<Simd<isize, 8>> for__m512i
implFrom<Simd<u8, 16>> for__m128i
implFrom<Simd<u8, 32>> for__m256i
implFrom<Simd<u8, 64>> for__m512i
implFrom<Simd<u16, 8>> for__m128i
implFrom<Simd<u16, 16>> for__m256i
implFrom<Simd<u16, 32>> for__m512i
implFrom<Simd<u32, 4>> for__m128i
implFrom<Simd<u32, 8>> for__m256i
implFrom<Simd<u32, 16>> for__m512i
implFrom<Simd<u64, 2>> for__m128i
implFrom<Simd<u64, 4>> for__m256i
implFrom<Simd<u64, 8>> for__m512i
implFrom<Simd<usize, 2>> for__m128i
implFrom<Simd<usize, 4>> for__m256i
implFrom<Simd<usize, 8>> for__m512i
implFrom<[u8;4]> forIpAddr
implFrom<[u8;4]> forIpv4Addr
implFrom<[u8;16]> forIpAddr
implFrom<[u8;16]> forIpv6Addr
implFrom<[u16;8]> forIpAddr
implFrom<[u16;8]> forIpv6Addr
impl<'a, T>From<&'aOption<T>> forOption<&'a T>
impl<'a, T>From<&'a mutOption<T>> forOption<&'a mut T>
impl<'data>From<&'data mut [u8]> forBorrowedBuf<'data>
Creates a newBorrowedBuf from a fully initialized slice.
impl<'data>From<&'data mut [MaybeUninit<u8>]> forBorrowedBuf<'data>
Creates a newBorrowedBuf from an uninitialized buffer.
Useset_init if part of the buffer is known to be already initialized.
impl<'data>From<BorrowedCursor<'data>> forBorrowedBuf<'data>
Creates a newBorrowedBuf from a cursor.
UseBorrowedCursor::with_unfilled_buf instead for a safer alternative.
impl<I:Into<IpAddr>>From<(I,u16)> forSocketAddr
impl<T>From<[T; N]> for(T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.
impl<T>From<!> for T
Stability note: This impl does not yet exist, but we are“reserving space” to add it in the future. Seerust-lang/rust#64715 for details.
impl<T>From<*mut T> forAtomicPtr<T>
impl<T>From<(T₁, T₂, …, Tₙ)> for[T; N]
This trait is implemented for tuples up to twelve items long.