pub trait TryFrom<T>:Sized { typeError; // Required method fntry_from(value: T) ->Result<Self, Self::Error>;}Expand description
Simple and safe type conversions that may fail in a controlledway under some circumstances. It is the reciprocal ofTryInto.
This is useful when you are doing a type conversion that maytrivially succeed but may also need special handling.For example, there is no way to convert ani64 into ani32using theFrom trait, because ani64 may contain a valuethat ani32 cannot represent and so the conversion would lose data.This might be handled by truncating thei64 to ani32 or bysimply returningi32::MAX, or by some other method. TheFromtrait is intended for perfect conversions, so theTryFrom traitinforms the programmer when a type conversion could go bad and letsthem decide how to handle it.
§Generic Implementations
TryFrom<T> for UimpliesTryInto<U> for Ttry_fromis reflexive, which means thatTryFrom<T> for Tis implemented and cannot fail – the associatedErrortype forcallingT::try_from()on a value of typeTisInfallible.When the!type is stabilizedInfallibleand!will beequivalent.
Prefer usingTryInto overTryFrom when specifying trait bounds on a generic functionto ensure that types that only implementTryInto can be used as well.
TryFrom<T> can be implemented as follows:
structGreaterThanZero(i32);implTryFrom<i32>forGreaterThanZero {typeError =&'staticstr;fntry_from(value: i32) ->Result<Self,Self::Error> {ifvalue <=0{Err("GreaterThanZero only accepts values greater than zero!") }else{Ok(GreaterThanZero(value)) } }}§Examples
As described,i32 implementsTryFrom<i64>:
letbig_number =1_000_000_000_000i64;// Silently truncates `big_number`, requires detecting// and handling the truncation after the fact.letsmaller_number = big_numberasi32;assert_eq!(smaller_number, -727379968);// Returns an error because `big_number` is too big to// fit in an `i32`.lettry_smaller_number = i32::try_from(big_number);assert!(try_smaller_number.is_err());// Returns `Ok(3)`.lettry_successful_smaller_number = i32::try_from(3);assert!(try_successful_smaller_number.is_ok());Required Associated Types§
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§
1.59.0 (const:unstable) ·Source§implTryFrom<char> foru8
Maps achar with code point in U+0000..=U+00FF to a byte in 0x00..=0xFF with same value,failing if the code point is greater than U+00FF.
implTryFrom<char> foru8
Maps achar with code point in U+0000..=U+00FF to a byte in 0x00..=0xFF with same value,failing if the code point is greater than U+00FF.
Seeimpl From<u8> for char for details on the encoding.
1.74.0 (const:unstable) ·Source§implTryFrom<char> foru16
Maps achar with code point in U+0000..=U+FFFF to au16 in 0x0000..=0xFFFF with same value,failing if the code point is greater than U+FFFF.
implTryFrom<char> foru16
Maps achar with code point in U+0000..=U+FFFF to au16 in 0x0000..=0xFFFF with same value,failing if the code point is greater than U+FFFF.
This corresponds to the UCS-2 encoding, as specified in ISO/IEC 10646:2003.
1.34.0 (const:unstable) ·Source§impl<'a, T, const N:usize>TryFrom<&'a[T]> for &'a[T; N]
Tries to create an array ref&[T; N] from a slice ref&[T]. Succeeds ifslice.len() == N.
impl<'a, T, const N:usize>TryFrom<&'a[T]> for &'a[T; N]
Tries to create an array ref&[T; N] from a slice ref&[T]. Succeeds ifslice.len() == N.
1.34.0 (const:unstable) ·Source§impl<'a, T, const N:usize>TryFrom<&'a mut[T]> for &'a mut[T; N]
Tries to create a mutable array ref&mut [T; N] from a mutable slice ref&mut [T]. Succeeds ifslice.len() == N.
impl<'a, T, const N:usize>TryFrom<&'a mut[T]> for &'a mut[T; N]
Tries to create a mutable array ref&mut [T; N] from a mutable slice ref&mut [T]. Succeeds ifslice.len() == N.
1.34.0 (const:unstable) ·Source§impl<T, U>TryFrom<U> for Twhere U:Into<T>,
impl<T, U>TryFrom<U> for Twhere U:Into<T>,
typeError =Infallible
1.34.0 (const:unstable) ·Source§impl<T, const N:usize>TryFrom<&[T]> for[T; N]where T:Copy,
Tries to create an array[T; N] by copying from a slice&[T].Succeeds ifslice.len() == N.
impl<T, const N:usize>TryFrom<&[T]> for[T; N]where T:Copy,
Tries to create an array[T; N] by copying from a slice&[T].Succeeds ifslice.len() == N.
1.59.0 (const:unstable) ·Source§impl<T, const N:usize>TryFrom<&mut[T]> for[T; N]where T:Copy,
Tries to create an array[T; N] by copying from a mutable slice&mut [T].Succeeds ifslice.len() == N.
impl<T, const N:usize>TryFrom<&mut[T]> for[T; N]where T:Copy,
Tries to create an array[T; N] by copying from a mutable slice&mut [T].Succeeds ifslice.len() == N.