简体   繁体   English

特征名称后面的特征是什么意思?

[英]What does the trait after the trait name mean?

I came across this trait definition while reading about Rust:我在阅读 Rust 时遇到了这个 trait 定义:

trait Enchanter: std::fmt::Debug {
    ...
}

From this I understand that the name of the trait is Enchanter , but I do not understand what the std::Format:Debug part implies since it is also a trait (I think).由此我了解到特征的名称是Enchanter ,但我不明白std::Format:Debug部分意味着什么,因为它也是一个特征(我认为)。

This is declaring a supertrait .这是声明一个supertrait It is equivalent to:它相当于:

trait Enchanter
where
    Self: std::fmt::Debug,
{
}

In short, it requires any type that wants to implement Enchanter to also implementstd::fmt::Debug .简而言之,它需要任何想要实现Enchanter的类型也实现std::fmt::Debug Otherwise, an error will be raised :否则, 将引发错误

error[E0277]: `S` doesn't implement `Debug`
 --> src/lib.rs:4:6
  |
4 | impl Enchanter for S {}
  |      ^^^^^^^^^ `S` cannot be formatted using `{:?}`
  |
  = help: the trait `Debug` is not implemented for `S`
  = note: add `#[derive(Debug)]` to `S` or manually `impl Debug for S`
note: required by a bound in `Enchanter`
 --> src/lib.rs:1:18
  |
1 | trait Enchanter: std::fmt::Debug {}
  |                  ^^^^^^^^^^^^^^^ required by this bound in `Enchanter`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM