Trait core::convert::From

1.0.0 · source ·
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)
}
Run

Required Methods§

source

fn from(value: T) -> Self

从输入类型转换为此类型。

Implementors§

1.36.0 · source§

impl From<Infallible> for TryFromSliceError

1.34.0 · source§

impl From<Infallible> for TryFromIntError

1.68.0 · source§

impl From<bool> for f32

1.68.0 · source§

impl From<bool> for f64

1.28.0 · source§

impl From<bool> for i8

1.28.0 · source§

impl From<bool> for i16

1.28.0 · source§

impl From<bool> for i32

1.28.0 · source§

impl From<bool> for i64

1.28.0 · source§

impl From<bool> for i128

1.28.0 · source§

impl From<bool> for isize

1.28.0 · source§

impl From<bool> for u8

1.28.0 · source§

impl From<bool> for u16

1.28.0 · source§

impl From<bool> for u32

1.28.0 · source§

impl From<bool> for u64

1.28.0 · source§

impl From<bool> for u128

1.28.0 · source§

impl From<bool> for usize

1.24.0 · source§

impl From<bool> for AtomicBool

1.13.0 · source§

impl From<char> for u32

1.51.0 · source§

impl From<char> for u64

1.51.0 · source§

impl From<char> for u128

1.6.0 · source§

impl From<f32> for f64

1.6.0 · source§

impl From<i8> for f32

1.6.0 · source§

impl From<i8> for f64

1.5.0 · source§

impl From<i8> for i16

1.5.0 · source§

impl From<i8> for i32

1.5.0 · source§

impl From<i8> for i64

1.26.0 · source§

impl From<i8> for i128

1.5.0 · source§

impl From<i8> for isize

1.34.0 · source§

impl From<i8> for AtomicI8

1.6.0 · source§

impl From<i16> for f32

1.6.0 · source§

impl From<i16> for f64

1.5.0 · source§

impl From<i16> for i32

1.5.0 · source§

impl From<i16> for i64

1.26.0 · source§

impl From<i16> for i128

1.26.0 · source§

impl From<i16> for isize

1.34.0 · source§

impl From<i16> for AtomicI16

1.6.0 · source§

impl From<i32> for f64

1.5.0 · source§

impl From<i32> for i64

1.26.0 · source§

impl From<i32> for i128

1.34.0 · source§

impl From<i32> for AtomicI32

1.26.0 · source§

impl From<i64> for i128

1.34.0 · source§

impl From<i64> for AtomicI64

1.23.0 · source§

impl From<isize> for AtomicIsize

1.34.0 · source§

impl From<!> for Infallible

source§

impl From<!> for TryFromIntError

1.13.0 · source§

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 上 asciiiso-8859-1windows-1252 都是 Windows-1252 超集的别名,该超集用相应的 C0 和 C1 控制代码填充了其余的空白。

1.6.0 · source§

impl From<u8> for f32

1.6.0 · source§

impl From<u8> for f64

1.5.0 · source§

impl From<u8> for i16

1.5.0 · source§

impl From<u8> for i32

1.5.0 · source§

impl From<u8> for i64

1.26.0 · source§

impl From<u8> for i128

1.26.0 · source§

impl From<u8> for isize

1.5.0 · source§

impl From<u8> for u16

1.5.0 · source§

impl From<u8> for u32

1.5.0 · source§

impl From<u8> for u64

1.26.0 · source§

impl From<u8> for u128

1.5.0 · source§

impl From<u8> for usize

1.34.0 · source§

impl From<u8> for AtomicU8

1.6.0 · source§

impl From<u16> for f32

1.6.0 · source§

impl From<u16> for f64

1.5.0 · source§

impl From<u16> for i32

1.5.0 · source§

impl From<u16> for i64

1.26.0 · source§

impl From<u16> for i128

1.5.0 · source§

impl From<u16> for u32

1.5.0 · source§

impl From<u16> for u64

1.26.0 · source§

impl From<u16> for u128

1.26.0 · source§

impl From<u16> for usize

1.34.0 · source§

impl From<u16> for AtomicU16

1.6.0 · source§

impl From<u32> for f64

1.5.0 · source§

impl From<u32> for i64

1.26.0 · source§

impl From<u32> for i128

1.5.0 · source§

impl From<u32> for u64

1.26.0 · source§

impl From<u32> for u128

1.1.0 · source§

impl From<u32> for Ipv4Addr

1.34.0 · source§

impl From<u32> for AtomicU32

1.26.0 · source§

impl From<u64> for i128

1.26.0 · source§

impl From<u64> for u128

1.34.0 · source§

impl From<u64> for AtomicU64

1.26.0 · source§

impl From<u128> for Ipv6Addr

1.23.0 · source§

impl From<usize> for AtomicUsize

source§

impl From<__m128> for f32x4

source§

impl From<__m128d> for f64x2

source§

impl From<__m128i> for i8x16

source§

impl From<__m128i> for i16x8

source§

impl From<__m128i> for i32x4

source§

impl From<__m128i> for i64x2

source§

impl From<__m128i> for isizex2

source§

impl From<__m128i> for u8x16

source§

impl From<__m128i> for u16x8

source§

impl From<__m128i> for u32x4

source§

impl From<__m128i> for u64x2

source§

impl From<__m128i> for usizex2

source§

impl From<__m256> for f32x8

source§

impl From<__m256d> for f64x4

source§

impl From<__m256i> for i8x32

source§

impl From<__m256i> for i16x16

source§

impl From<__m256i> for i32x8

source§

impl From<__m256i> for i64x4

source§

impl From<__m256i> for isizex4

source§

impl From<__m256i> for u8x32

source§

impl From<__m256i> for u16x16

source§

impl From<__m256i> for u32x8

source§

impl From<__m256i> for u64x4

source§

impl From<__m256i> for usizex4

source§

impl From<__m512> for f32x16

source§

impl From<__m512d> for f64x8

source§

impl From<__m512i> for i8x64

source§

impl From<__m512i> for i16x32

source§

impl From<__m512i> for i32x16

source§

impl From<__m512i> for i64x8

source§

impl From<__m512i> for isizex8

source§

impl From<__m512i> for u8x64

source§

impl From<__m512i> for u16x32

source§

impl From<__m512i> for u32x16

source§

impl From<__m512i> for u64x8

source§

impl From<__m512i> for usizex8

1.16.0 · source§

impl From<Ipv4Addr> for IpAddr

1.1.0 · source§

impl From<Ipv4Addr> for u32

1.16.0 · source§

impl From<Ipv6Addr> for IpAddr

1.26.0 · source§

impl From<Ipv6Addr> for u128

1.16.0 · source§

impl From<SocketAddrV4> for SocketAddr

1.16.0 · source§

impl From<SocketAddrV6> for SocketAddr

1.31.0 · source§

impl From<NonZeroI8> for i8

1.41.0 · source§

impl From<NonZeroI8> for NonZeroI16

1.41.0 · source§

impl From<NonZeroI8> for NonZeroI32

1.41.0 · source§

impl From<NonZeroI8> for NonZeroI64

1.41.0 · source§

impl From<NonZeroI8> for NonZeroI128

1.41.0 · source§

impl From<NonZeroI8> for NonZeroIsize

1.31.0 · source§

impl From<NonZeroI16> for i16

1.41.0 · source§

impl From<NonZeroI16> for NonZeroI32

1.41.0 · source§

impl From<NonZeroI16> for NonZeroI64

1.41.0 · source§

impl From<NonZeroI16> for NonZeroI128

1.41.0 · source§

impl From<NonZeroI16> for NonZeroIsize

1.31.0 · source§

impl From<NonZeroI32> for i32

1.41.0 · source§

impl From<NonZeroI32> for NonZeroI64

1.41.0 · source§

impl From<NonZeroI32> for NonZeroI128

1.31.0 · source§

impl From<NonZeroI64> for i64

1.41.0 · source§

impl From<NonZeroI64> for NonZeroI128

1.31.0 · source§

impl From<NonZeroI128> for i128

1.31.0 · source§

impl From<NonZeroIsize> for isize

1.31.0 · source§

impl From<NonZeroU8> for u8

1.41.0 · source§

impl From<NonZeroU8> for NonZeroI16

1.41.0 · source§

impl From<NonZeroU8> for NonZeroI32

1.41.0 · source§

impl From<NonZeroU8> for NonZeroI64

1.41.0 · source§

impl From<NonZeroU8> for NonZeroI128

1.41.0 · source§

impl From<NonZeroU8> for NonZeroIsize

1.41.0 · source§

impl From<NonZeroU8> for NonZeroU16

1.41.0 · source§

impl From<NonZeroU8> for NonZeroU32

1.41.0 · source§

impl From<NonZeroU8> for NonZeroU64

1.41.0 · source§

impl From<NonZeroU8> for NonZeroU128

1.41.0 · source§

impl From<NonZeroU8> for NonZeroUsize

1.31.0 · source§

impl From<NonZeroU16> for u16

1.41.0 · source§

impl From<NonZeroU16> for NonZeroI32

1.41.0 · source§

impl From<NonZeroU16> for NonZeroI64

1.41.0 · source§

impl From<NonZeroU16> for NonZeroI128

1.41.0 · source§

impl From<NonZeroU16> for NonZeroU32

1.41.0 · source§

impl From<NonZeroU16> for NonZeroU64

1.41.0 · source§

impl From<NonZeroU16> for NonZeroU128

1.41.0 · source§

impl From<NonZeroU16> for NonZeroUsize

1.31.0 · source§

impl From<NonZeroU32> for u32

1.41.0 · source§

impl From<NonZeroU32> for NonZeroI64

1.41.0 · source§

impl From<NonZeroU32> for NonZeroI128

1.41.0 · source§

impl From<NonZeroU32> for NonZeroU64

1.41.0 · source§

impl From<NonZeroU32> for NonZeroU128

1.31.0 · source§

impl From<NonZeroU64> for u64

1.41.0 · source§

impl From<NonZeroU64> for NonZeroI128

1.41.0 · source§

impl From<NonZeroU64> for NonZeroU128

1.31.0 · source§

impl From<NonZeroU128> for u128

1.31.0 · source§

impl From<NonZeroUsize> for usize

source§

impl From<Alignment> for usize

source§

impl From<Alignment> for NonZeroUsize

source§

impl From<Simd<f32, 4>> for __m128

source§

impl From<Simd<f32, 8>> for __m256

source§

impl From<Simd<f32, 16>> for __m512

source§

impl From<Simd<f64, 2>> for __m128d

source§

impl From<Simd<f64, 4>> for __m256d

source§

impl From<Simd<f64, 8>> for __m512d

source§

impl From<Simd<i8, 16>> for __m128i

source§

impl From<Simd<i8, 32>> for __m256i

source§

impl From<Simd<i8, 64>> for __m512i

source§

impl From<Simd<i16, 8>> for __m128i

source§

impl From<Simd<i16, 16>> for __m256i

source§

impl From<Simd<i16, 32>> for __m512i

source§

impl From<Simd<i32, 4>> for __m128i

source§

impl From<Simd<i32, 8>> for __m256i

source§

impl From<Simd<i32, 16>> for __m512i

source§

impl From<Simd<i64, 2>> for __m128i

source§

impl From<Simd<i64, 4>> for __m256i

source§

impl From<Simd<i64, 8>> for __m512i

source§

impl From<Simd<isize, 2>> for __m128i

source§

impl From<Simd<isize, 4>> for __m256i

source§

impl From<Simd<isize, 8>> for __m512i

source§

impl From<Simd<u8, 16>> for __m128i

source§

impl From<Simd<u8, 32>> for __m256i

source§

impl From<Simd<u8, 64>> for __m512i

source§

impl From<Simd<u16, 8>> for __m128i

source§

impl From<Simd<u16, 16>> for __m256i

source§

impl From<Simd<u16, 32>> for __m512i

source§

impl From<Simd<u32, 4>> for __m128i

source§

impl From<Simd<u32, 8>> for __m256i

source§

impl From<Simd<u32, 16>> for __m512i

source§

impl From<Simd<u64, 2>> for __m128i

source§

impl From<Simd<u64, 4>> for __m256i

source§

impl From<Simd<u64, 8>> for __m512i

source§

impl From<Simd<usize, 2>> for __m128i

source§

impl From<Simd<usize, 4>> for __m256i

source§

impl From<Simd<usize, 8>> for __m512i

1.17.0 · source§

impl From<[u8; 4]> for IpAddr

1.9.0 · source§

impl From<[u8; 4]> for Ipv4Addr

1.17.0 · source§

impl From<[u8; 16]> for IpAddr

1.9.0 · source§

impl From<[u8; 16]> for Ipv6Addr

1.17.0 · source§

impl From<[u16; 8]> for IpAddr

1.16.0 · source§

impl From<[u16; 8]> for Ipv6Addr

1.30.0 · source§

impl<'a, T> From<&'a Option<T>> for Option<&'a T>

1.30.0 · source§

impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>

1.17.0 · source§

impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr

1.71.0 · source§

impl<T> From<[T; 1]> for (T,)

1.71.0 · source§

impl<T> From<[T; 2]> for (T, T)

1.71.0 · source§

impl<T> From<[T; 3]> for (T, T, T)

1.71.0 · source§

impl<T> From<[T; 4]> for (T, T, T, T)

1.71.0 · source§

impl<T> From<[T; 5]> for (T, T, T, T, T)

1.71.0 · source§

impl<T> From<[T; 6]> for (T, T, T, T, T, T)

1.71.0 · source§

impl<T> From<[T; 7]> for (T, T, T, T, T, T, T)

1.71.0 · source§

impl<T> From<[T; 8]> for (T, T, T, T, T, T, T, T)

1.71.0 · source§

impl<T> From<[T; 9]> for (T, T, T, T, T, T, T, T, T)

1.71.0 · source§

impl<T> From<[T; 10]> for (T, T, T, T, T, T, T, T, T, T)

1.71.0 · source§

impl<T> From<[T; 11]> for (T, T, T, T, T, T, T, T, T, T, T)

1.71.0 · source§

impl<T> From<[T; 12]> for (T, T, T, T, T, T, T, T, T, T, T, T)

1.34.0 · source§

impl<T> From<!> for T

稳定性注意事项: 该 impl 尚不存在,但我们 “保留空间” 以在将来添加它。 有关详细信息,请参见 rust-lang/rust#64715

1.23.0 · source§

impl<T> From<*mut T> for AtomicPtr<T>

1.71.0 · source§

impl<T> From<(T, T)> for [T; 2]

1.71.0 · source§

impl<T> From<(T, T, T)> for [T; 3]

1.71.0 · source§

impl<T> From<(T, T, T, T)> for [T; 4]

1.71.0 · source§

impl<T> From<(T, T, T, T, T)> for [T; 5]

1.71.0 · source§

impl<T> From<(T, T, T, T, T, T)> for [T; 6]

1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T)> for [T; 7]

1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T)> for [T; 8]

1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T, T)> for [T; 9]

1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T, T, T)> for [T; 10]

1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T, T, T, T)> for [T; 11]

1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T, T, T, T, T)> for [T; 12]

1.71.0 · source§

impl<T> From<(T,)> for [T; 1]

1.12.0 · source§

impl<T> From<T> for Option<T>

1.36.0 · source§

impl<T> From<T> for Poll<T>

1.12.0 · source§

impl<T> From<T> for Cell<T>

1.70.0 · source§

impl<T> From<T> for OnceCell<T>

1.12.0 · source§

impl<T> From<T> for RefCell<T>

source§

impl<T> From<T> for SyncUnsafeCell<T>

1.12.0 · source§

impl<T> From<T> for UnsafeCell<T>

source§

impl<T> From<T> for Exclusive<T>

source§

impl<T> From<T> for T

source§

impl<T, const LANES: usize> From<Mask<T, LANES>> for [bool; LANES]where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

source§

impl<T, const LANES: usize> From<[bool; LANES]> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

source§

impl<T, const N: usize> From<[T; N]> for Simd<T, N>where LaneCount<N>: SupportedLaneCount, T: SimdElement,

source§

impl<T, const N: usize> From<Simd<T, N>> for [T; N]where LaneCount<N>: SupportedLaneCount, T: SimdElement,

1.25.0 · source§

impl<T: ?Sized> From<&T> for NonNull<T>

1.25.0 · source§

impl<T: ?Sized> From<&mut T> for NonNull<T>

source§

impl<const LANES: usize> From<Mask<i8, LANES>> for Mask<i16, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i8, LANES>> for Mask<i32, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i8, LANES>> for Mask<i64, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i8, LANES>> for Mask<isize, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i16, LANES>> for Mask<i8, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i16, LANES>> for Mask<i32, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i16, LANES>> for Mask<i64, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i16, LANES>> for Mask<isize, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i32, LANES>> for Mask<i8, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i32, LANES>> for Mask<i16, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i32, LANES>> for Mask<i64, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i32, LANES>> for Mask<isize, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i64, LANES>> for Mask<i8, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i64, LANES>> for Mask<i16, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i64, LANES>> for Mask<i32, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<i64, LANES>> for Mask<isize, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<isize, LANES>> for Mask<i8, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<isize, LANES>> for Mask<i16, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<isize, LANES>> for Mask<i32, LANES>where LaneCount<LANES>: SupportedLaneCount,

source§

impl<const LANES: usize> From<Mask<isize, LANES>> for Mask<i64, LANES>where LaneCount<LANES>: SupportedLaneCount,