知识卡片
类型名字只适合诊断
type_name::()返回编译期T的诊断名称,经泛型调用也不会自动识别dyn背后的具体类型。输出尽力而为,可能变化且不保证唯一,因此不应解析它做类型判断、协议标识或业务路由。需要受控类型识别时应考虑TypeId或Any。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Item 19: Avoid reflection · 查看原书依据
type_name::<T>() } The implementation is provided by the std::any::type_name<T> library function, which is also generic. This function has access only to compile-time information; there is no code run that determines the type at runtime. Returning to the trait object types used in Item 12 demonstrates this: let square = Square::new(1, 2, 2); let draw: &dyn D
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Item 19: Avoid reflection》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u107-ch03-046)。
- 结论依据:原文在“type_name”附近的定义、例子或代码支撑本卡核心说明:type_name::<T>()返回编译期T的诊断名称,经泛型调用也不会自动识别dyn背后的具体类型。边界按已读前提解释;示例未执行。
- 原始内容:type_name::<T>() } The implementation is provided by the std::any::type_name<T> library function, which is also generic. This function has access only to compile-time information; there is no code run that determines the type at runtime. Returning to the trait object types used in Item 12 demonstrates this: let square = Square::new(1, 2, 2); let draw: &dyn D