pub trait From<T>: Sized {
// Required method
fn from(value: T) -> Self;
}
Expand description
用于在消耗输入值的同时进行值到值的转换。它是 Into
的倒数。
与标准 Into
相比,人们应该总是更喜欢实现 From
,因为由于标准库中的全面实现,实现 From
会自动为 Into
提供一个 Into
的实现。
仅当针对 Rust 1.41 之前的版本并将其转换为当前 crate 以外的类型时,才实现 Into
。
由于 Rust 的孤儿规则,From
在早期版本中无法进行这些类型的转换。
有关更多详细信息,请参见 Into
。
在泛型函数上指定 trait bounds 时,优先使用 Into
。
这样,直接实现 Into
的类型也可以用作参数。
From
在执行错误处理时也非常有用。当创建一个能够失败的函数时,返回类型通常为 Result<T, E>
形式。
From
trait 通过允许函数返回封装了多种错误类型的单个错误类型,简化了错误处理。有关更多详细信息,请参见示例部分和这本 书。
注意:此 trait 一定不能失败。From
trait 旨在实现完美转换。
如果转换失败或不完美,请使用 TryFrom
。
泛型实现
From<T> for U
意味着Into
<U> for T
From
是反射的,这意味着From<T> for T
被实现
Examples
String
实现 From<&str>
:
从 &str
到字符串的显式转换如下:
let string = "hello".to_string();
let other_string = String::from("hello");
assert_eq!(string, other_string);
Run在执行错误处理时,通常对于您自己的错误类型实现 From
很有用。
通过将底层错误类型转换为封装底层错误类型的自定义错误类型,我们可以返回单个错误类型,而不会丢失有关底层原因的信息。
‘?’ 运算符通过调用 Into<CliError>::into
自动将底层错误类型转换为我们的自定义错误类型,该 Into<CliError>::into
是在实现 From
时自动提供的。
然后,编译器会推断应使用 Into
的哪种实现。
use std::fs;
use std::io;
use std::num;
enum CliError {
IoError(io::Error),
ParseError(num::ParseIntError),
}
impl From<io::Error> for CliError {
fn from(error: io::Error) -> Self {
CliError::IoError(error)
}
}
impl From<num::ParseIntError> for CliError {
fn from(error: num::ParseIntError) -> Self {
CliError::ParseError(error)
}
}
fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
let mut contents = fs::read_to_string(&file_name)?;
let num: i32 = contents.trim().parse()?;
Ok(num)
}
RunRequired Methods§
Implementors§
impl From<Infallible> for TryFromSliceError
impl From<Infallible> for TryFromIntError
impl From<bool> for f32
impl From<bool> for f64
impl From<bool> for i8
impl From<bool> for i16
impl From<bool> for i32
impl From<bool> for i64
impl From<bool> for i128
impl From<bool> for isize
impl From<bool> for u8
impl From<bool> for u16
impl From<bool> for u32
impl From<bool> for u64
impl From<bool> for u128
impl From<bool> for usize
impl From<bool> for AtomicBool
impl From<char> for u32
impl From<char> for u64
impl From<char> for u128
impl From<f32> for f64
impl From<i8> for f32
impl From<i8> for f64
impl From<i8> for i16
impl From<i8> for i32
impl From<i8> for i64
impl From<i8> for i128
impl From<i8> for isize
impl From<i8> for AtomicI8
impl From<i16> for f32
impl From<i16> for f64
impl From<i16> for i32
impl From<i16> for i64
impl From<i16> for i128
impl From<i16> for isize
impl From<i16> for AtomicI16
impl From<i32> for f64
impl From<i32> for i64
impl From<i32> for i128
impl From<i32> for AtomicI32
impl From<i64> for i128
impl From<i64> for AtomicI64
impl From<isize> for AtomicIsize
impl From<!> for Infallible
impl From<!> for TryFromIntError
impl From<u8> for char
将 0x00..=0xFF 中的字节映射到 char
,该 char
的代码点具有相同的值,即 U+0000..=U+00FF。
Unicode 的设计使其可以使用 IANA 称为 ISO-8859-1 的字符编码有效地解码字节。 此编码与 ASCII 兼容。
请注意,这与 ISO/IEC 8859-1 又名不同 ISO 8859-1 (连字符少一个),它留下了一些 “blanks” 字节值,这些值未分配给任何字符。 ISO-8859-1 (属于 IANA) 将它们分配给 C0 和 C1 控制代码。
请注意,这也与 Windows-1252 也不同 代码页 1252,它是 ISO/IEC 8859-1 的超集,它为标点符号和各种拉丁字符分配了一些 (不是全部) 空格。
为了进一步混淆,在 Web 上 ascii
,iso-8859-1
和 windows-1252
都是 Windows-1252 超集的别名,该超集用相应的 C0 和 C1 控制代码填充了其余的空白。
impl From<u8> for f32
impl From<u8> for f64
impl From<u8> for i16
impl From<u8> for i32
impl From<u8> for i64
impl From<u8> for i128
impl From<u8> for isize
impl From<u8> for u16
impl From<u8> for u32
impl From<u8> for u64
impl From<u8> for u128
impl From<u8> for usize
impl From<u8> for AtomicU8
impl From<u16> for f32
impl From<u16> for f64
impl From<u16> for i32
impl From<u16> for i64
impl From<u16> for i128
impl From<u16> for u32
impl From<u16> for u64
impl From<u16> for u128
impl From<u16> for usize
impl From<u16> for AtomicU16
impl From<u32> for f64
impl From<u32> for i64
impl From<u32> for i128
impl From<u32> for u64
impl From<u32> for u128
impl From<u32> for Ipv4Addr
impl From<u32> for AtomicU32
impl From<u64> for i128
impl From<u64> for u128
impl From<u64> for AtomicU64
impl From<u128> for Ipv6Addr
impl From<usize> for AtomicUsize
impl From<__m128> for f32x4
impl From<__m128d> for f64x2
impl From<__m128i> for i8x16
impl From<__m128i> for i16x8
impl From<__m128i> for i32x4
impl From<__m128i> for i64x2
impl From<__m128i> for isizex2
impl From<__m128i> for u8x16
impl From<__m128i> for u16x8
impl From<__m128i> for u32x4
impl From<__m128i> for u64x2
impl From<__m128i> for usizex2
impl From<__m256> for f32x8
impl From<__m256d> for f64x4
impl From<__m256i> for i8x32
impl From<__m256i> for i16x16
impl From<__m256i> for i32x8
impl From<__m256i> for i64x4
impl From<__m256i> for isizex4
impl From<__m256i> for u8x32
impl From<__m256i> for u16x16
impl From<__m256i> for u32x8
impl From<__m256i> for u64x4
impl From<__m256i> for usizex4
impl From<__m512> for f32x16
impl From<__m512d> for f64x8
impl From<__m512i> for i8x64
impl From<__m512i> for i16x32
impl From<__m512i> for i32x16
impl From<__m512i> for i64x8
impl From<__m512i> for isizex8
impl From<__m512i> for u8x64
impl From<__m512i> for u16x32
impl From<__m512i> for u32x16
impl From<__m512i> for u64x8
impl From<__m512i> for usizex8
impl From<Ipv4Addr> for IpAddr
impl From<Ipv4Addr> for u32
impl From<Ipv6Addr> for IpAddr
impl From<Ipv6Addr> for u128
impl From<SocketAddrV4> for SocketAddr
impl From<SocketAddrV6> for SocketAddr
impl From<NonZeroI8> for i8
impl From<NonZeroI8> for NonZeroI16
impl From<NonZeroI8> for NonZeroI32
impl From<NonZeroI8> for NonZeroI64
impl From<NonZeroI8> for NonZeroI128
impl From<NonZeroI8> for NonZeroIsize
impl From<NonZeroI16> for i16
impl From<NonZeroI16> for NonZeroI32
impl From<NonZeroI16> for NonZeroI64
impl From<NonZeroI16> for NonZeroI128
impl From<NonZeroI16> for NonZeroIsize
impl From<NonZeroI32> for i32
impl From<NonZeroI32> for NonZeroI64
impl From<NonZeroI32> for NonZeroI128
impl From<NonZeroI64> for i64
impl From<NonZeroI64> for NonZeroI128
impl From<NonZeroI128> for i128
impl From<NonZeroIsize> for isize
impl From<NonZeroU8> for u8
impl From<NonZeroU8> for NonZeroI16
impl From<NonZeroU8> for NonZeroI32
impl From<NonZeroU8> for NonZeroI64
impl From<NonZeroU8> for NonZeroI128
impl From<NonZeroU8> for NonZeroIsize
impl From<NonZeroU8> for NonZeroU16
impl From<NonZeroU8> for NonZeroU32
impl From<NonZeroU8> for NonZeroU64
impl From<NonZeroU8> for NonZeroU128
impl From<NonZeroU8> for NonZeroUsize
impl From<NonZeroU16> for u16
impl From<NonZeroU16> for NonZeroI32
impl From<NonZeroU16> for NonZeroI64
impl From<NonZeroU16> for NonZeroI128
impl From<NonZeroU16> for NonZeroU32
impl From<NonZeroU16> for NonZeroU64
impl From<NonZeroU16> for NonZeroU128
impl From<NonZeroU16> for NonZeroUsize
impl From<NonZeroU32> for u32
impl From<NonZeroU32> for NonZeroI64
impl From<NonZeroU32> for NonZeroI128
impl From<NonZeroU32> for NonZeroU64
impl From<NonZeroU32> for NonZeroU128
impl From<NonZeroU64> for u64
impl From<NonZeroU64> for NonZeroI128
impl From<NonZeroU64> for NonZeroU128
impl From<NonZeroU128> for u128
impl From<NonZeroUsize> for usize
impl From<Alignment> for usize
impl From<Alignment> for NonZeroUsize
impl From<Simd<f32, 4>> for __m128
impl From<Simd<f32, 8>> for __m256
impl From<Simd<f32, 16>> for __m512
impl From<Simd<f64, 2>> for __m128d
impl From<Simd<f64, 4>> for __m256d
impl From<Simd<f64, 8>> for __m512d
impl From<Simd<i8, 16>> for __m128i
impl From<Simd<i8, 32>> for __m256i
impl From<Simd<i8, 64>> for __m512i
impl From<Simd<i16, 8>> for __m128i
impl From<Simd<i16, 16>> for __m256i
impl From<Simd<i16, 32>> for __m512i
impl From<Simd<i32, 4>> for __m128i
impl From<Simd<i32, 8>> for __m256i
impl From<Simd<i32, 16>> for __m512i
impl From<Simd<i64, 2>> for __m128i
impl From<Simd<i64, 4>> for __m256i
impl From<Simd<i64, 8>> for __m512i
impl From<Simd<isize, 2>> for __m128i
impl From<Simd<isize, 4>> for __m256i
impl From<Simd<isize, 8>> for __m512i
impl From<Simd<u8, 16>> for __m128i
impl From<Simd<u8, 32>> for __m256i
impl From<Simd<u8, 64>> for __m512i
impl From<Simd<u16, 8>> for __m128i
impl From<Simd<u16, 16>> for __m256i
impl From<Simd<u16, 32>> for __m512i
impl From<Simd<u32, 4>> for __m128i
impl From<Simd<u32, 8>> for __m256i
impl From<Simd<u32, 16>> for __m512i
impl From<Simd<u64, 2>> for __m128i
impl From<Simd<u64, 4>> for __m256i
impl From<Simd<u64, 8>> for __m512i
impl From<Simd<usize, 2>> for __m128i
impl From<Simd<usize, 4>> for __m256i
impl From<Simd<usize, 8>> for __m512i
impl From<[u8; 4]> for IpAddr
impl From<[u8; 4]> for Ipv4Addr
impl From<[u8; 16]> for IpAddr
impl From<[u8; 16]> for Ipv6Addr
impl From<[u16; 8]> for IpAddr
impl From<[u16; 8]> for Ipv6Addr
impl<'a, T> From<&'a Option<T>> for Option<&'a T>
impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr
impl<T> From<[T; 1]> for (T,)
impl<T> From<[T; 2]> for (T, T)
impl<T> From<[T; 3]> for (T, T, T)
impl<T> From<[T; 4]> for (T, T, T, T)
impl<T> From<[T; 5]> for (T, T, T, T, T)
impl<T> From<[T; 6]> for (T, T, T, T, T, T)
impl<T> From<[T; 7]> for (T, T, T, T, T, T, T)
impl<T> From<[T; 8]> for (T, T, T, T, T, T, T, T)
impl<T> From<[T; 9]> for (T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 10]> for (T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 11]> for (T, T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 12]> for (T, T, T, T, T, T, T, T, T, T, T, T)
impl<T> From<!> for T
稳定性注意事项: 该 impl 尚不存在,但我们 “保留空间” 以在将来添加它。 有关详细信息,请参见 rust-lang/rust#64715。