TryInto
TryInto is a trait in the Rust standard library that represents fallible conversions from one type to another. It is defined in std::convert and is used for conversions that can fail at runtime, returning a Result rather than performing a guaranteed cast.
TryInto is automatically implemented for a type T when the destination type U implements TryFrom<T>. In practice,
let y: Result<u8, _> = x.try_into();
If the value fits in the target type, you get Ok(v); otherwise you receive an Err with
Error handling and usage patterns:
The error type is associated with the destination type’s TryFrom implementation, and is often something like
fn to_u8(n: i32) -> Result<u8, <u8 as std::convert::TryFrom<i32>>::Error> {
}
TryInto is a convenient counterpart to TryFrom. If U implements TryFrom<T>, then T automatically implements TryInto<U>.
TryInto enforces that the value actually fits in the target type and returns an error on failure,