试图解决Trait bound Sized is not satisfied for Sized trait中描述的问题时,我发现以下代码给出了以下错误:

trait SizedTrait: Sized {
    fn me() -> Self;
}

trait AnotherTrait: Sized {
    fn another_me() -> Self;
}

impl AnotherTrait for SizedTrait + Sized {
    fn another_me() {
        Self::me()
    }
}
error[E0225]: only auto traits can be used as additional traits in a trait object
 --> src/main.rs:9:36
  |
9 | impl AnotherTrait for SizedTrait + Sized {
  |                                    ^^^^^ non-auto additional trait

Rust Book人根本没有提到auto trait人.

rust 病中的自体特征是什么?它与非自体特征有何不同?

推荐答案

An auto trait is the new name for the terribly named1 opt-in, built-in trait (OIBIT).

这是一个不稳定的特性,其中一个特性会自动为每种类型实现,除非它们 Select 退出或包含一个不实现该特性的值:

#![feature(optin_builtin_traits)]

auto trait IsCool {}

// Everyone knows that `String`s just aren't cool
impl !IsCool for String {}

struct MyStruct;
struct HasAString(String);

fn check_cool<C: IsCool>(_: C) {}

fn main() {
    check_cool(42);
    check_cool(false);
    check_cool(MyStruct);
    
    // the trait bound `std::string::String: IsCool` is not satisfied
    // check_cool(String::new());
    
    // the trait bound `std::string::String: IsCool` is not satisfied in `HasAString`
    // check_cool(HasAString(String::new()));
}

常见的例子包括SendSync:

pub unsafe auto trait Send { }
pub unsafe auto trait Sync { }

有关更多信息,请参阅Unstable Book页.


1这些特征既不是 Select 性加入(它们是 Select 性退出),也不一定是内置的(夜间使用的用户代码可能会使用它们).在他们名字中的5个词中,有4个是彻头彻尾的谎言.

Rust相关问答推荐

如何在Rust中为具有多个数据持有者的enum变体编写文档 comments ?

Rust kill std::processs::child

trait声明中的生命周期参数

我如何在Rust中使用传递依赖中的特征?

当两者都有效时,为什么Rust编译器建议添加';&;而不是';*';?

这种获取-释放关系是如何运作的?

"value is never read警告似乎不正确.我应该忽略它吗?

在Rust中显式装箱受生存期限制的转换闭包

从Type::new()调用函数

铁 rust ,我的模块介绍突然遇到了一个问题

如何go 除多余的(0..)在迭代中,当它不被使用时?

Rust移动/复制涉及实际复制时进行检测

更新 rust ndarray 中矩阵的一行

Rust 中指向自身的引用如何工作?

实现AsyncWrite到hyper Sender时发生生命周期错误

为什么要这样编译?

borrow 匹配手臂内部的可变

打印 `format_args!` 时borrow 时临时值丢失

当 T 不是副本时,为什么取消引用 Box 不会抱怨移出共享引用?

意外的正则表达式模式匹配