什么是Rust的汽车特性?

Mar*_*tin 15 rust

试图解决Trait bound Sized中描述的问题对于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()
    }
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

Rust Book根本没有提到auto trait.

Rust中的自动特性是什么?它与非自动特征有何不同?

She*_*ter 23

一个自动特点是针对非常命名的新名称1 选择加入,内置性状(OIBIT).

这是一个不稳定的特性,除非它们选择退出或包含不实现特征的值,否则将自动为每种类型实现特征:

#![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()));
}
Run Code Online (Sandbox Code Playgroud)

熟悉的例子包括SendSync:

pub unsafe auto trait Send { }
pub unsafe auto trait Sync { }
Run Code Online (Sandbox Code Playgroud)

更多信息可在不稳定的书中找到.


1这些特征既不是选择性的(它们是选择性的)也不一定是内置的(用户代码每晚使用它们).在他们名下的5个单词中,有4个是彻头彻尾的谎言.