pub trait From<T>: Sized {
fn from(_: T) -> Self;
}
Expand description
用于在消耗输入值的同时进行值到值的转换。它是 Into
的倒数。
与标准 Into
相比,人们应该总是更喜欢实现 From
,因为由于标准库中的全面实现,实现 From
会自动为 Into
提供一个 Into
的实现。
仅当针对 Rust 1.41 之前的版本并将其转换为当前 crate 以外的类型时,才实现 Into
。
From
由于 Rust 的孤儿规则,无法在较早版本中进行这些类型的转换。
有关更多详细信息,请参见 Into
。
在泛型函数上指定 trait bounds 时,优先使用 Into
。
这样,直接实现 Into
的类型也可以用作参数。
From
在执行错误处理时也非常有用。当创建一个能够失败的函数时,返回类型通常为 Result<T, E>
形式。
From
trait 通过允许函数返回封装了多种错误类型的单个错误类型,简化了错误处理。有关更多详细信息,请参见 “示例” 部分和这本 书。
注意: 此 trait 一定不能失败。如果转换失败,请使用 TryFrom
。
From<T> for U
暗示 Into
<U> for T
From
是自反的,这意味着 From<T> for T
已实现
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
Converts NonZeroI8
to NonZeroI16
losslessly.
Converts NonZeroI8
to NonZeroI32
losslessly.
Converts NonZeroI8
to NonZeroI64
losslessly.
Converts NonZeroI8
to NonZeroI128
losslessly.
Converts NonZeroI8
to NonZeroIsize
losslessly.
Converts a NonZeroI8
into an i8
Converts NonZeroI16
to NonZeroI32
losslessly.
Converts NonZeroI16
to NonZeroI64
losslessly.
Converts NonZeroI16
to NonZeroI128
losslessly.
Converts NonZeroI16
to NonZeroIsize
losslessly.
Converts a NonZeroI16
into an i16
Converts NonZeroI32
to NonZeroI64
losslessly.
Converts NonZeroI32
to NonZeroI128
losslessly.
Converts a NonZeroI32
into an i32
Converts NonZeroI64
to NonZeroI128
losslessly.
Converts a NonZeroI64
into an i64
Converts a NonZeroI128
into an i128
Converts a NonZeroIsize
into an isize
Converts NonZeroU8
to NonZeroI16
losslessly.
Converts NonZeroU8
to NonZeroI32
losslessly.
Converts NonZeroU8
to NonZeroI64
losslessly.
Converts NonZeroU8
to NonZeroI128
losslessly.
Converts NonZeroU8
to NonZeroIsize
losslessly.
Converts NonZeroU8
to NonZeroU16
losslessly.
Converts NonZeroU8
to NonZeroU32
losslessly.
Converts NonZeroU8
to NonZeroU64
losslessly.
Converts NonZeroU8
to NonZeroU128
losslessly.
Converts NonZeroU8
to NonZeroUsize
losslessly.
Converts a NonZeroU8
into an u8
Converts NonZeroU16
to NonZeroI32
losslessly.
Converts NonZeroU16
to NonZeroI64
losslessly.
Converts NonZeroU16
to NonZeroI128
losslessly.
Converts NonZeroU16
to NonZeroU32
losslessly.
Converts NonZeroU16
to NonZeroU64
losslessly.
Converts NonZeroU16
to NonZeroU128
losslessly.
Converts NonZeroU16
to NonZeroUsize
losslessly.
Converts a NonZeroU16
into an u16
Converts NonZeroU32
to NonZeroI64
losslessly.
Converts NonZeroU32
to NonZeroI128
losslessly.
Converts NonZeroU32
to NonZeroU64
losslessly.
Converts NonZeroU32
to NonZeroU128
losslessly.
Converts a NonZeroU32
into an u32
Converts NonZeroU64
to NonZeroI128
losslessly.
Converts NonZeroU64
to NonZeroU128
losslessly.
Converts a NonZeroU64
into an u64
Converts a NonZeroU128
into an u128
Converts a NonZeroUsize
into an usize
将 bool
转换为 AtomicBool
。
use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{:?}", atomic_bool), "true")
Run
Converts a bool
to a i8
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(i8::from(true), 1);
assert_eq!(i8::from(false), 0);
Run
Converts a bool
to a i16
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(i16::from(true), 1);
assert_eq!(i16::from(false), 0);
Run
Converts a bool
to a i32
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(i32::from(true), 1);
assert_eq!(i32::from(false), 0);
Run
Converts a bool
to a i64
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(i64::from(true), 1);
assert_eq!(i64::from(false), 0);
Run
Converts a bool
to a i128
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(i128::from(true), 1);
assert_eq!(i128::from(false), 0);
Run
Converts a bool
to a isize
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(isize::from(true), 1);
assert_eq!(isize::from(false), 0);
Run
Converts a bool
to a u8
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(u8::from(true), 1);
assert_eq!(u8::from(false), 0);
Run
Converts a bool
to a u16
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(u16::from(true), 1);
assert_eq!(u16::from(false), 0);
Run
Converts a bool
to a u32
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(u32::from(true), 1);
assert_eq!(u32::from(false), 0);
Run
Converts a bool
to a u64
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(u64::from(true), 1);
assert_eq!(u64::from(false), 0);
Run
Converts a bool
to a u128
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(u128::from(true), 1);
assert_eq!(u128::from(false), 0);
Run
Converts a bool
to a usize
. The resulting value is 0
for false
and 1
for true
values.
assert_eq!(usize::from(true), 1);
assert_eq!(usize::from(false), 0);
Run
将 char
转换为 u32
。
use std::mem;
let c = 'c';
let u = u32::from(c);
assert!(4 == mem::size_of_val(&u))
Run
将 char
转换为 u64
。
use std::mem;
let c = '👤';
let u = u64::from(c);
assert!(8 == mem::size_of_val(&u))
Run
将 char
转换为 u128
。
use std::mem;
let c = '⚙';
let u = u128::from(c);
assert!(16 == mem::size_of_val(&u))
Run
Converts f32
to f64
losslessly.
Converts an i8
into an AtomicI8
.
Converts i8
to f32
losslessly.
Converts i8
to f64
losslessly.
Converts i8
to i16
losslessly.
Converts i8
to i32
losslessly.
Converts i8
to i64
losslessly.
Converts i8
to i128
losslessly.
Converts i8
to isize
losslessly.
Converts an i16
into an AtomicI16
.
Converts i16
to f32
losslessly.
Converts i16
to f64
losslessly.
Converts i16
to i32
losslessly.
Converts i16
to i64
losslessly.
Converts i16
to i128
losslessly.
Converts i16
to isize
losslessly.
Converts an i32
into an AtomicI32
.
Converts i32
to f64
losslessly.
Converts i32
to i64
losslessly.
Converts i32
to i128
losslessly.
Converts an i64
into an AtomicI64
.
Converts i64
to i128
losslessly.
Converts an isize
into an AtomicIsize
.
Converts an u8
into an AtomicU8
.
将 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 控制代码填充了其余的空白。
将 u8
转换为 char
。
use std::mem;
let u = 32 as u8;
let c = char::from(u);
assert!(4 == mem::size_of_val(&c))
Run
Converts u8
to f32
losslessly.
Converts u8
to f64
losslessly.
Converts u8
to i16
losslessly.
Converts u8
to i32
losslessly.
Converts u8
to i64
losslessly.
Converts u8
to i128
losslessly.
Converts u8
to isize
losslessly.
Converts u8
to u16
losslessly.
Converts u8
to u32
losslessly.
Converts u8
to u64
losslessly.
Converts u8
to u128
losslessly.
Converts u8
to usize
losslessly.
Converts an u16
into an AtomicU16
.
Converts u16
to f32
losslessly.
Converts u16
to f64
losslessly.
Converts u16
to i32
losslessly.
Converts u16
to i64
losslessly.
Converts u16
to i128
losslessly.
Converts u16
to u32
losslessly.
Converts u16
to u64
losslessly.
Converts u16
to u128
losslessly.
Converts u16
to usize
losslessly.
Converts an u32
into an AtomicU32
.
Converts u32
to f64
losslessly.
Converts u32
to i64
losslessly.
Converts u32
to i128
losslessly.
Converts u32
to u64
losslessly.
Converts u32
to u128
losslessly.
Converts an u64
into an AtomicU64
.
Converts u64
to i128
losslessly.
Converts u64
to u128
losslessly.
Converts an usize
into an AtomicUsize
.
从 &Option<T>
转换为 Option<&T>
。
将 Option<
String
>
转换为 Option<
usize
>
,并保留原始格式。
map
方法按值取 self
参数,消耗原始值,因此该技术使用 from
首先取 Option
到原始值内部值的引用。
let s: Option<String> = Some(String::from("Hello, Rustaceans!"));
let o: Option<usize> = Option::from(&s).map(|ss: &String| ss.len());
println!("Can still print s: {:?}", s);
assert_eq!(o, Some(18));
Run
从 &mut Option<T>
转换为 Option<&mut T>
let mut s = Some(String::from("Hello"));
let o: Option<&mut String> = Option::from(&mut s);
match o {
Some(t) => *t = String::from("Hello, Rustaceans!"),
None => (),
}
assert_eq!(s, Some(String::from("Hello, Rustaceans!")));
Run
将 val
复制到新的 Some
中。
let o: Option<u8> = Option::from(67);
assert_eq!(Some(67), o);
Run
转换为 Ready
成员。
assert_eq!(Poll::from(true), Poll::Ready(true));
Run