Trait core::convert::TryFrom 1.34.0[−][src]
Expand description
简单安全的类型转换在某些情况下可能会以受控方式失败。它是 TryInto 的倒数。
当您进行的类型转换可能会成功完成但可能还需要特殊处理时,这很有用。
例如,无法使用 From trait 将 i64 转换为 i32,因为 i64 可能包含 i32 无法表示的值,因此转换将丢失数据。
这可以通过将 i64 截断为 i32 (本质上给 i64 的值取 i32::MAX 模) 或通过简单地返回 i32::MAX 或其他方法来处理。
From trait 用于完美的转换,因此 TryFrom trait 会通知程序员类型转换何时会变差,并让他们决定如何处理它。
泛型实现
TryFrom<T> for U暗示TryInto<U> for Ttry_from是自反的,这意味着TryFrom<T> for T已实现且不会失败 - 用于在T类型的值上调用T::try_from()的关联Error类型为Infallible。 当!类型稳定后,Infallible和!将等效。
TryFrom<T> 可以实现如下:
use std::convert::TryFrom; struct GreaterThanZero(i32); impl TryFrom<i32> for GreaterThanZero { type Error = &'static str; fn try_from(value: i32) -> Result<Self, Self::Error> { if value <= 0 { Err("GreaterThanZero only accepts value superior than zero!") } else { Ok(GreaterThanZero(value)) } } }Run
Examples
use std::convert::TryFrom; let big_number = 1_000_000_000_000i64; // 默默地截断 `big_number`,事实之后需要检测并处理该截断。 let smaller_number = big_number as i32; assert_eq!(smaller_number, -727379968); // 由于 `big_number` 太大而无法容纳在 `i32` 中,因此返回错误。 let try_smaller_number = i32::try_from(big_number); assert!(try_smaller_number.is_err()); // 返回 `Ok(3)`。 let try_successful_smaller_number = i32::try_from(3); assert!(try_successful_smaller_number.is_ok());Run
Associated Types
Required methods
Implementors
Attempts to convert NonZeroI16 to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroI16 to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroI16 to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroI16 to NonZeroU32.
type Error = TryFromIntError
Attempts to convert NonZeroI16 to NonZeroU64.
type Error = TryFromIntError
Attempts to convert NonZeroI16 to NonZeroU128.
type Error = TryFromIntError
Attempts to convert NonZeroI16 to NonZeroUsize.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroIsize.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroU32.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroU64.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroU128.
type Error = TryFromIntError
Attempts to convert NonZeroI32 to NonZeroUsize.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroI32.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroIsize.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroU32.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroU64.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroU128.
type Error = TryFromIntError
Attempts to convert NonZeroI64 to NonZeroUsize.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroI32.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroI64.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroIsize.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroU32.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroU64.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroU128.
type Error = TryFromIntError
Attempts to convert NonZeroI128 to NonZeroUsize.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroI32.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroI64.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroI128.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroU32.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroU64.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroU128.
type Error = TryFromIntError
Attempts to convert NonZeroIsize to NonZeroUsize.
type Error = TryFromIntError
Attempts to convert NonZeroU16 to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroU16 to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroU16 to NonZeroIsize.
type Error = TryFromIntError
Attempts to convert NonZeroU16 to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroU32 to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroU32 to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroU32 to NonZeroI32.
type Error = TryFromIntError
Attempts to convert NonZeroU32 to NonZeroIsize.
type Error = TryFromIntError
Attempts to convert NonZeroU32 to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroU32 to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroU32 to NonZeroUsize.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroI32.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroI64.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroIsize.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroU32.
type Error = TryFromIntError
Attempts to convert NonZeroU64 to NonZeroUsize.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroI32.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroI64.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroI128.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroIsize.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroU32.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroU64.
type Error = TryFromIntError
Attempts to convert NonZeroU128 to NonZeroUsize.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroI8.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroI16.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroI32.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroI64.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroI128.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroIsize.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroU8.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroU16.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroU32.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroU64.
type Error = TryFromIntError
Attempts to convert NonZeroUsize to NonZeroU128.