知识卡片

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

新增默认方法仍需检查冲突

理解等级:高级

为已发布trait增加默认方法通常无需所有实现者补代码,但仍可能与其他trait的同名方法冲突。固有方法通常优先,必要时可用::method明确选择。默认实现减少维护负担,不等于任何新增API都不会影响现有调用。

这张卡片看不懂?

已有反馈 · 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-024)。 - 结论依据:原文在“new method name”附近的定义、例子或代码支撑本卡核心说明:为已发布trait增加默认方法通常无需所有实现者补代码,但仍可能与其他trait的同名方法冲突。边界按已读前提解释;示例未执行。 - 原始内容:new method name does not clash with the name of a method from some other trait that the type implements.8 So follow the example of the standard library and provide a minimal API surface for implementors but a convenient and comprehensive API for users, by adding methods with default implementations (and trait bounds as appropriate). 1 Of course, comparing fl