知识卡片
分派方式兼顾能力与成本
泛型保留具体类型并易组合多个trait约束,可能增加实例化与编译成本;dyn擦除类型并经间接调用,常可减少重复代码。差异是否重要需测量,不能保证某种总更快或更小。若需Debug+Draw等多种能力组合,还应比较泛型约束与额外组合trait的接口复杂度。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 2. Traits Basic Comparisons · 查看原书依据
Basic Comparisons These basic facts already allow some immediate comparisons between the two possibilities: Generics are likely to lead to bigger code sizes, because the compiler generates a fresh copy (on_screen::<T>(&T)) of the code for every type T that uses the generic version of the on_screen function. In contrast, the trait object version (on_screen(&d
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Basic Comparisons》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u071-ch02-018)。
- 结论依据:原文在“Basic Comparisons”附近的定义、例子或代码支撑本卡核心说明:泛型保留具体类型并易组合多个trait约束,可能增加实例化与编译成本;dyn擦除类型并经间接调用,常可减少重复代码。边界按已读前提解释;示例未执行。
- 原始内容:Basic Comparisons These basic facts already allow some immediate comparisons between the two possibilities: Generics are likely to lead to bigger code sizes, because the compiler generates a fresh copy (on_screen::<T>(&T)) of the code for every type T that uses the generic version of the on_screen function. In contrast, the trait object version (on_screen(&d