Enum std::convert::Infallible1.34.0[][src]

pub enum Infallible {}
Expand description

永远不会发生的错误的错误类型。

由于此枚举没有成员,因此这种类型的值永远不会实际存在。 这对于使用 Result 并参数化错误类型的泛型 API 很有用,以指示结果始终为 Ok

例如,对于存在反向 Into 实现的所有类型,TryFrom trait (返回 Result 的转换) 都具有通用实现。

impl<T, U> TryFrom<U> for T where U: Into<T> {
    type Error = Infallible;

    fn try_from(value: U) -> Result<Self, Infallible> {
        Ok(U::into(value))  // 永不返回 `Err`
    }
}
Run

Future 兼容性

该枚举与 never 类型 (!) 具有相同的作用,在此版本的 Rust 中不稳定。 当 ! 稳定后,我们计划将 Infallible 用作它的类型别名:

pub type Infallible = !;
Run

… 并最终弃用 Infallible

但是,在一种情况下,可以在将 ! 稳定为完整类型之前使用 ! 语法: 在函数的返回类型位置。 具体来说,可能有两种不同的函数指针类型的实现:

trait MyTrait {}
impl MyTrait for fn() -> ! {}
impl MyTrait for fn() -> std::convert::Infallible {}
Run

Infallible 是枚举,此代码有效。 但是,当 Infallible 成为 never type 的别名时,两个 impl 将开始重叠,因此将被语言的 trait 一致性规则所禁止。

Trait Implementations

返回值的副本。 Read more

source 执行复制分配。 Read more

使用给定的格式化程序格式化该值。 Read more

使用给定的格式化程序格式化该值。 Read more

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

此错误的下级来源 (如果有)。 Read more

🔬 This is a nightly-only experimental API. (backtrace #53487)

返回发生错误的栈回溯 (如果有)。 Read more

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

执行转换。

执行转换。

执行转换。

将该值输入给定的 HasherRead more

将这种类型的切片送入给定的 Hasher 中。 Read more

此方法返回 selfother 之间的 OrderingRead more

比较并返回两个值中的最大值。 Read more

比较并返回两个值中的最小值。 Read more

将值限制为一定的时间间隔。 Read more

此方法测试 selfother 值是否相等,并由 == 使用。 Read more

此方法测试 !=

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

执行转换。

获得所有权后的结果类型。

通常通过克隆从借用数据中创建拥有的数据。 Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

recently added

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more

将给定值转换为 StringRead more

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。