知识卡片

普通读书笔记卡 · 6074.d.1.a.6

少量原语支撑便利默认方法

理解等级:中级

trait实现者希望必需方法少,使用者希望便利方法多。可将能由原语推导的操作写成默认实现,让实现者只完成核心能力,并在必要时覆盖优化。默认方法也可加额外约束,只有元素可Clone时才开放cloned等操作,不把所有能力强加给每个实现者。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Item 13: Use default implementations to minimize required trait methods》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u075-ch02-023)。 - 结论依据:原文在“default implementations”附近的定义、例子或代码支撑本卡核心说明:trait实现者希望必需方法少,使用者希望便利方法多。边界按已读前提解释;示例未执行。 - 原始内容:default implementations to minimize required trait methods The designer of a trait has two different audiences to consider: the programmers who will be implementing the trait and those who will be using the trait. These two audiences lead to a degree of tension in the trait design: To make the implementor’s life easier, it’s better for a trait to have the ab