pub trait TryFrom<T>: Sized {
type Error;
// Required method
fn try_from(value: T) -> Result<Self, Self::Error>;
}
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 T
try_from
是反射的,这意味着TryFrom<T> for T
已实现并且不会失败 – 用于在类型T
上调用T::try_from()
的关联Error
类型是Infallible
。 当!
类型稳定后,Infallible
和!
将等效。
TryFrom<T>
可以实现如下:
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 values greater than zero!")
} else {
Ok(GreaterThanZero(value))
}
}
}
RunExamples
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());
RunRequired Associated Types§
Required Methods§
Implementors§
1.59.0 · source§impl TryFrom<char> for u8
impl TryFrom<char> for u8
将 U+0000..=U+00FF 中代码点的 char
映射到 0x00..=0xFF 中具有相同值的字节,如果代码点大于 U+00FF 则失败。
有关编码的详细信息,请参见 impl From<u8> for char
。
type Error = TryFromCharError
1.46.0 · source§impl TryFrom<i16> for NonZeroI16
impl TryFrom<i16> for NonZeroI16
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<i32> for NonZeroI32
impl TryFrom<i32> for NonZeroI32
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<i64> for NonZeroI64
impl TryFrom<i64> for NonZeroI64
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<i128> for NonZeroI128
impl TryFrom<i128> for NonZeroI128
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<isize> for NonZeroIsize
impl TryFrom<isize> for NonZeroIsize
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<u16> for NonZeroU16
impl TryFrom<u16> for NonZeroU16
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<u32> for NonZeroU32
impl TryFrom<u32> for NonZeroU32
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<u64> for NonZeroU64
impl TryFrom<u64> for NonZeroU64
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<u128> for NonZeroU128
impl TryFrom<u128> for NonZeroU128
type Error = TryFromIntError
1.46.0 · source§impl TryFrom<usize> for NonZeroUsize
impl TryFrom<usize> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI8> for NonZeroU16
impl TryFrom<NonZeroI8> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI8> for NonZeroU32
impl TryFrom<NonZeroI8> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI8> for NonZeroU64
impl TryFrom<NonZeroI8> for NonZeroU64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI8> for NonZeroU128
impl TryFrom<NonZeroI8> for NonZeroU128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI8> for NonZeroUsize
impl TryFrom<NonZeroI8> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI16> for NonZeroI8
impl TryFrom<NonZeroI16> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI16> for NonZeroU8
impl TryFrom<NonZeroI16> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI16> for NonZeroU16
impl TryFrom<NonZeroI16> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI16> for NonZeroU32
impl TryFrom<NonZeroI16> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI16> for NonZeroU64
impl TryFrom<NonZeroI16> for NonZeroU64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI16> for NonZeroU128
impl TryFrom<NonZeroI16> for NonZeroU128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI16> for NonZeroUsize
impl TryFrom<NonZeroI16> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroI8
impl TryFrom<NonZeroI32> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroI16
impl TryFrom<NonZeroI32> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroIsize
impl TryFrom<NonZeroI32> for NonZeroIsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroU8
impl TryFrom<NonZeroI32> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroU16
impl TryFrom<NonZeroI32> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroU32
impl TryFrom<NonZeroI32> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroU64
impl TryFrom<NonZeroI32> for NonZeroU64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroU128
impl TryFrom<NonZeroI32> for NonZeroU128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI32> for NonZeroUsize
impl TryFrom<NonZeroI32> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroI8
impl TryFrom<NonZeroI64> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroI16
impl TryFrom<NonZeroI64> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroI32
impl TryFrom<NonZeroI64> for NonZeroI32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroIsize
impl TryFrom<NonZeroI64> for NonZeroIsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroU8
impl TryFrom<NonZeroI64> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroU16
impl TryFrom<NonZeroI64> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroU32
impl TryFrom<NonZeroI64> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroU64
impl TryFrom<NonZeroI64> for NonZeroU64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroU128
impl TryFrom<NonZeroI64> for NonZeroU128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI64> for NonZeroUsize
impl TryFrom<NonZeroI64> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroI8
impl TryFrom<NonZeroI128> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroI16
impl TryFrom<NonZeroI128> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroI32
impl TryFrom<NonZeroI128> for NonZeroI32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroI64
impl TryFrom<NonZeroI128> for NonZeroI64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroIsize
impl TryFrom<NonZeroI128> for NonZeroIsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroU8
impl TryFrom<NonZeroI128> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroU16
impl TryFrom<NonZeroI128> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroU32
impl TryFrom<NonZeroI128> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroU64
impl TryFrom<NonZeroI128> for NonZeroU64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroU128
impl TryFrom<NonZeroI128> for NonZeroU128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroI128> for NonZeroUsize
impl TryFrom<NonZeroI128> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroI8
impl TryFrom<NonZeroIsize> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroI16
impl TryFrom<NonZeroIsize> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroI32
impl TryFrom<NonZeroIsize> for NonZeroI32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroI64
impl TryFrom<NonZeroIsize> for NonZeroI64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroI128
impl TryFrom<NonZeroIsize> for NonZeroI128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroU8
impl TryFrom<NonZeroIsize> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroU16
impl TryFrom<NonZeroIsize> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroU32
impl TryFrom<NonZeroIsize> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroU64
impl TryFrom<NonZeroIsize> for NonZeroU64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroU128
impl TryFrom<NonZeroIsize> for NonZeroU128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroIsize> for NonZeroUsize
impl TryFrom<NonZeroIsize> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU16> for NonZeroI8
impl TryFrom<NonZeroU16> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU16> for NonZeroI16
impl TryFrom<NonZeroU16> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU16> for NonZeroIsize
impl TryFrom<NonZeroU16> for NonZeroIsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU16> for NonZeroU8
impl TryFrom<NonZeroU16> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU32> for NonZeroI8
impl TryFrom<NonZeroU32> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU32> for NonZeroI16
impl TryFrom<NonZeroU32> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU32> for NonZeroI32
impl TryFrom<NonZeroU32> for NonZeroI32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU32> for NonZeroIsize
impl TryFrom<NonZeroU32> for NonZeroIsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU32> for NonZeroU8
impl TryFrom<NonZeroU32> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU32> for NonZeroU16
impl TryFrom<NonZeroU32> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU32> for NonZeroUsize
impl TryFrom<NonZeroU32> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroI8
impl TryFrom<NonZeroU64> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroI16
impl TryFrom<NonZeroU64> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroI32
impl TryFrom<NonZeroU64> for NonZeroI32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroI64
impl TryFrom<NonZeroU64> for NonZeroI64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroIsize
impl TryFrom<NonZeroU64> for NonZeroIsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroU8
impl TryFrom<NonZeroU64> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroU16
impl TryFrom<NonZeroU64> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroU32
impl TryFrom<NonZeroU64> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU64> for NonZeroUsize
impl TryFrom<NonZeroU64> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroI8
impl TryFrom<NonZeroU128> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroI16
impl TryFrom<NonZeroU128> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroI32
impl TryFrom<NonZeroU128> for NonZeroI32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroI64
impl TryFrom<NonZeroU128> for NonZeroI64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroI128
impl TryFrom<NonZeroU128> for NonZeroI128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroIsize
impl TryFrom<NonZeroU128> for NonZeroIsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroU8
impl TryFrom<NonZeroU128> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroU16
impl TryFrom<NonZeroU128> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroU32
impl TryFrom<NonZeroU128> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroU64
impl TryFrom<NonZeroU128> for NonZeroU64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroU128> for NonZeroUsize
impl TryFrom<NonZeroU128> for NonZeroUsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroI8
impl TryFrom<NonZeroUsize> for NonZeroI8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroI16
impl TryFrom<NonZeroUsize> for NonZeroI16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroI32
impl TryFrom<NonZeroUsize> for NonZeroI32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroI64
impl TryFrom<NonZeroUsize> for NonZeroI64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroI128
impl TryFrom<NonZeroUsize> for NonZeroI128
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroIsize
impl TryFrom<NonZeroUsize> for NonZeroIsize
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroU8
impl TryFrom<NonZeroUsize> for NonZeroU8
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroU16
impl TryFrom<NonZeroUsize> for NonZeroU16
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroU32
impl TryFrom<NonZeroUsize> for NonZeroU32
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroU64
impl TryFrom<NonZeroUsize> for NonZeroU64
type Error = TryFromIntError
1.49.0 · source§impl TryFrom<NonZeroUsize> for NonZeroU128
impl TryFrom<NonZeroUsize> for NonZeroU128
type Error = TryFromIntError
source§impl TryFrom<NonZeroUsize> for Alignment
impl TryFrom<NonZeroUsize> for Alignment
type Error = TryFromIntError
source§impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
尝试从切片 ref &[T]
创建数组 ref &[T; N]
。
如果 slice.len() == N
则成功。
let bytes: [u8; 3] = [1, 0, 2];
let bytes_head: &[u8; 2] = <&[u8; 2]>::try_from(&bytes[0..2]).unwrap();
assert_eq!(1, u16::from_le_bytes(*bytes_head));
let bytes_tail: &[u8; 2] = bytes[1..3].try_into().unwrap();
assert_eq!(512, u16::from_le_bytes(*bytes_tail));
Runtype Error = TryFromSliceError
source§impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
尝试从一个非线性切片 ref &mut [T]
创建一个非线性数组 ref &mut [T; N]
。
如果 slice.len() == N
则成功。
let mut bytes: [u8; 3] = [1, 0, 2];
let bytes_head: &mut [u8; 2] = <&mut [u8; 2]>::try_from(&mut bytes[0..2]).unwrap();
assert_eq!(1, u16::from_le_bytes(*bytes_head));
let bytes_tail: &mut [u8; 2] = (&mut bytes[1..3]).try_into().unwrap();
assert_eq!(512, u16::from_le_bytes(*bytes_tail));
Runtype Error = TryFromSliceError
source§impl<T, const N: usize> TryFrom<&[T]> for [T; N]where
T: Copy,
impl<T, const N: usize> TryFrom<&[T]> for [T; N]where T: Copy,
尝试通过从切片 &[T]
复制来创建数组 [T; N]
。
如果 slice.len() == N
则成功。
let bytes: [u8; 3] = [1, 0, 2];
let bytes_head: [u8; 2] = <[u8; 2]>::try_from(&bytes[0..2]).unwrap();
assert_eq!(1, u16::from_le_bytes(bytes_head));
let bytes_tail: [u8; 2] = bytes[1..3].try_into().unwrap();
assert_eq!(512, u16::from_le_bytes(bytes_tail));
Runtype Error = TryFromSliceError
source§impl<T, const N: usize> TryFrom<&[T]> for Simd<T, N>where
LaneCount<N>: SupportedLaneCount,
T: SimdElement,
impl<T, const N: usize> TryFrom<&[T]> for Simd<T, N>where LaneCount<N>: SupportedLaneCount, T: SimdElement,
type Error = TryFromSliceError
1.59.0 · source§impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]where
T: Copy,
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]where T: Copy,
尝试通过从无效切片 &mut [T]
复制来创建数组 [T; N]
。
如果 slice.len() == N
则成功。
let mut bytes: [u8; 3] = [1, 0, 2];
let bytes_head: [u8; 2] = <[u8; 2]>::try_from(&mut bytes[0..2]).unwrap();
assert_eq!(1, u16::from_le_bytes(bytes_head));
let bytes_tail: [u8; 2] = (&mut bytes[1..3]).try_into().unwrap();
assert_eq!(512, u16::from_le_bytes(bytes_tail));
Run