Trait std::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 T
try_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 i16
to NonZeroI16
.
type Error = TryFromIntError
Attempts to convert i32
to NonZeroI32
.
type Error = TryFromIntError
Attempts to convert i64
to NonZeroI64
.
type Error = TryFromIntError
Attempts to convert i128
to NonZeroI128
.
type Error = TryFromIntError
pub fn try_from(
value: isize
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<isize>>::Error>
[src]
pub fn try_from(
value: isize
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<isize>>::Error>
[src]Attempts to convert isize
to NonZeroIsize
.
type Error = TryFromIntError
Attempts to convert u16
to NonZeroU16
.
type Error = TryFromIntError
Attempts to convert u32
to NonZeroU32
.
type Error = TryFromIntError
Attempts to convert u64
to NonZeroU64
.
type Error = TryFromIntError
Attempts to convert u128
to NonZeroU128
.
type Error = TryFromIntError
pub fn try_from(
value: usize
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<usize>>::Error>
[src]
pub fn try_from(
value: usize
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<usize>>::Error>
[src]Attempts to convert usize
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI8>>::Error>
[src]
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI8>>::Error>
[src]Attempts to convert NonZeroI8
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI8>>::Error>
[src]
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI8>>::Error>
[src]Attempts to convert NonZeroI8
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI8>>::Error>
[src]
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI8>>::Error>
[src]Attempts to convert NonZeroI8
to NonZeroU64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI8>>::Error>
[src]
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI8>>::Error>
[src]Attempts to convert NonZeroI8
to NonZeroU128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI8>>::Error>
[src]
pub fn try_from(
value: NonZeroI8
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI8>>::Error>
[src]Attempts to convert NonZeroI8
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroI16>>::Error>
[src]
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroI16>>::Error>
[src]Attempts to convert NonZeroI16
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroI16>>::Error>
[src]
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroI16>>::Error>
[src]Attempts to convert NonZeroI16
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI16>>::Error>
[src]
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI16>>::Error>
[src]Attempts to convert NonZeroI16
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI16>>::Error>
[src]
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI16>>::Error>
[src]Attempts to convert NonZeroI16
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI16>>::Error>
[src]
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI16>>::Error>
[src]Attempts to convert NonZeroI16
to NonZeroU64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI16>>::Error>
[src]
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI16>>::Error>
[src]Attempts to convert NonZeroI16
to NonZeroU128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI16>>::Error>
[src]
pub fn try_from(
value: NonZeroI16
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI16>>::Error>
[src]Attempts to convert NonZeroI16
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroIsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroU64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroU128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI32>>::Error>
[src]
pub fn try_from(
value: NonZeroI32
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI32>>::Error>
[src]Attempts to convert NonZeroI32
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroI32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroIsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroU64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroU128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI64>>::Error>
[src]
pub fn try_from(
value: NonZeroI64
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI64>>::Error>
[src]Attempts to convert NonZeroI64
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroI32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroI64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroIsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroU64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroU128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI128>>::Error>
[src]
pub fn try_from(
value: NonZeroI128
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroI128>>::Error>
[src]Attempts to convert NonZeroI128
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroI32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroI64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI128, <NonZeroI128 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroI128, <NonZeroI128 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroI128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroU64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroU128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroIsize>>::Error>
[src]
pub fn try_from(
value: NonZeroIsize
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroIsize>>::Error>
[src]Attempts to convert NonZeroIsize
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU16
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroU16>>::Error>
[src]
pub fn try_from(
value: NonZeroU16
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroU16>>::Error>
[src]Attempts to convert NonZeroU16
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU16
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroU16>>::Error>
[src]
pub fn try_from(
value: NonZeroU16
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroU16>>::Error>
[src]Attempts to convert NonZeroU16
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU16
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroU16>>::Error>
[src]
pub fn try_from(
value: NonZeroU16
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroU16>>::Error>
[src]Attempts to convert NonZeroU16
to NonZeroIsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU16
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroU16>>::Error>
[src]
pub fn try_from(
value: NonZeroU16
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroU16>>::Error>
[src]Attempts to convert NonZeroU16
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroU32>>::Error>
[src]
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroU32>>::Error>
[src]Attempts to convert NonZeroU32
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroU32>>::Error>
[src]
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroU32>>::Error>
[src]Attempts to convert NonZeroU32
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroU32>>::Error>
[src]
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroU32>>::Error>
[src]Attempts to convert NonZeroU32
to NonZeroI32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroU32>>::Error>
[src]
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroU32>>::Error>
[src]Attempts to convert NonZeroU32
to NonZeroIsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroU32>>::Error>
[src]
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroU32>>::Error>
[src]Attempts to convert NonZeroU32
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroU32>>::Error>
[src]
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroU32>>::Error>
[src]Attempts to convert NonZeroU32
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroU32>>::Error>
[src]
pub fn try_from(
value: NonZeroU32
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroU32>>::Error>
[src]Attempts to convert NonZeroU32
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroI32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroI64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroIsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroU64>>::Error>
[src]
pub fn try_from(
value: NonZeroU64
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroU64>>::Error>
[src]Attempts to convert NonZeroU64
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroI32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroI64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI128, <NonZeroI128 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroI128, <NonZeroI128 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroI128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroIsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroU64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroU128>>::Error>
[src]
pub fn try_from(
value: NonZeroU128
) -> Result<NonZeroUsize, <NonZeroUsize as TryFrom<NonZeroU128>>::Error>
[src]Attempts to convert NonZeroU128
to NonZeroUsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI8, <NonZeroI8 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroI8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI16, <NonZeroI16 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroI16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI32, <NonZeroI32 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroI32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI64, <NonZeroI64 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroI64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI128, <NonZeroI128 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroI128, <NonZeroI128 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroI128
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroIsize, <NonZeroIsize as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroIsize
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU8, <NonZeroU8 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroU8
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU16, <NonZeroU16 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroU16
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU32, <NonZeroU32 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroU32
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU64, <NonZeroU64 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroU64
.
type Error = TryFromIntError
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroUsize>>::Error>
[src]
pub fn try_from(
value: NonZeroUsize
) -> Result<NonZeroU128, <NonZeroU128 as TryFrom<NonZeroUsize>>::Error>
[src]Attempts to convert NonZeroUsize
to NonZeroU128
.
type Error = TryFromIntError
type Error = TryFromSliceError
type Error = TryFromSliceError
type Error = TryFromSliceError
如果 Vec<T>
的大小与请求的数组的大小完全匹配,则以数组的形式获取 Vec<T>
的全部内容。
Examples
use std::convert::TryInto; assert_eq!(vec![1, 2, 3].try_into(), Ok([1, 2, 3])); assert_eq!(<Vec<i32>>::new().try_into(), Ok([]));Run
如果长度不匹配,则输入以 Err
返回:
use std::convert::TryInto; let r: Result<[i32; 4], _> = (0..10).collect::<Vec<_>>().try_into(); assert_eq!(r, Err(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));Run
如果只需要获得 Vec<T>
的前缀就可以了,您可以先调用 .truncate(N)
。
use std::convert::TryInto; let mut v = String::from("hello world").into_bytes(); v.sort(); v.truncate(2); let [a, b]: [_; 2] = v.try_into().unwrap(); assert_eq!(a, b' '); assert_eq!(b, b'd');Run