我在阅读 Rust 时遇到了这个特征定义:
trait Enchanter: std::fmt::Debug {
...
}
Run Code Online (Sandbox Code Playgroud)
由此我了解到该特征的名称是Enchanter,但我不明白该std::Format:Debug部分意味着什么,因为它也是一个特征(我认为)。
这是在宣告一个超级特质。它相当于:
trait Enchanter
where
Self: std::fmt::Debug,
{
}
Run Code Online (Sandbox Code Playgroud)
简而言之,它要求任何想要实现的类型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`
Run Code Online (Sandbox Code Playgroud)