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

发布于 2025-02-03 10:59:54 字数 213 浏览 2 评论 0 原文

我在阅读有关Rust的特征定义时遇到了这个特征的定义:

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

由此我了解该特征的名称是 nchanter ,但我不明白 std ::格式:debug std ::格式>部分意味着它也是一个特征(我认为)。

I came across this trait definition while reading about Rust:

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光暖心i 2025-02-10 10:59:55

这是在宣布a 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`

This is declaring a 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 implement 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`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文