← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.21 · 生成模型:未记录

父特征声明能力依赖

理解等级:中级

trait OutlinePrint: Display 表示每个实现 OutlinePrint 的类型也须提供 Display。OutlinePrint 的默认方法因此能依靠格式化能力调用 to_string,而不必为每种类型重写边框逻辑。类型缺少 Display 时,编译器会拒绝 OutlinePrint 的实现。这个约束表达两种行为间的依赖。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 高级特性 Disambiguating Between Methods with the Same Name · 查看原书依据

      In the implementation of the outline_print method, we want to use the Display trait’s functionality. Therefore, we need to specify that the OutlinePrint trait will work only for types that also implement Display and provide the functionality that OutlinePrint needs. We can do that in the trait definition by specifying OutlinePrint: Display. This technique is similar to adding a trait bound to the trait.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第19章 高级特性(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c19.xhtml`,块 0200) - 结论依据:原文用 OutlinePrint: Display 给出声明,并解释为何默认方法可复用 Display 的功能。 - 原始内容:In the implementation of the outline_print method, we want to use the Display trait’s functionality. Therefore, we need to specify that the OutlinePrint trait will work only for types that also implement Display and provide the functionality that OutlinePrint needs. We can do that in the trait definition by specifying OutlinePrint: Display. This technique is similar to adding a trait bound to the trait.