[英]What does the trait after the trait name mean?
我在阅读 Rust 时遇到了这个 trait 定义:
trait Enchanter: std::fmt::Debug {
...
}
由此我了解到特征的名称是Enchanter
,但我不明白std::Format:Debug
部分意味着什么,因为它也是一个特征(我认为)。
这是声明一个supertrait 。 它相当于:
trait Enchanter
where
Self: std::fmt::Debug,
{
}
简而言之,它需要任何想要实现Enchanter
的类型也实现std::fmt::Debug
。 否则, 将引发错误:
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.