知识卡片
泛型按具体类型实例化
泛型函数以trait bound声明所需能力,编译器按调用时具体类型单态化,使同一接口可静态调用Square或Circle的实现。参数位置impl Trait是简洁形式,但不提供同样的显式类型参数指定方式。最终机器代码还可能合并或内联,不能只按源码实例数推算体积。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 2. Traits Generics · 查看原书依据
monomorphization in Rust, and template instantiation in C++. Unlike C++, Rust explicitly encodes the expectations for the type T in the type system, in the form of trait bounds for the generic. For the example, a generic function that uses the trait’s bounds() method has an explicit Draw trait bound: /// Indicate whether an object is on-screen. pub fn on_scr
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Generics》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u069-ch02-017)。
- 结论依据:原文在“monomorphization”附近的定义、例子或代码支撑本卡核心说明:泛型函数以trait bound声明所需能力,编译器按调用时具体类型单态化,使同一接口可静态调用Square或Circle的实现。边界按已读前提解释;示例未执行。
- 原始内容:monomorphization in Rust, and template instantiation in C++. Unlike C++, Rust explicitly encodes the expectations for the type T in the type system, in the form of trait bounds for the generic. For the example, a generic function that uses the trait’s bounds() method has an explicit Draw trait bound: /// Indicate whether an object is on-screen. pub fn on_scr