知识卡片
Any提供受控具体类型识别
Any为满足’static约束的类型提供TypeId,并允许在dyn Any上检查类型或用downcast_ref、downcast_mut取得匹配的具体引用。失败需要处理,它不能枚举对象任意字段或查询全部trait实现。T:‘static限制借用内容,并不要求一个拥有值本身必须永远活着。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Item 19: Avoid reflection · 查看原书依据
Any trait instead, because the standard library has additional functionality for working with Any instances (described below). The Any trait has a single method type_id(), which returns the TypeId value for the type that implements the trait. You can’t implement this trait yourself, though, because Any already comes with a blanket implementation for most arb
参考来源
- 位置:《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-047)。
- 结论依据:原文在“Any trait”附近的定义、例子或代码支撑本卡核心说明:Any为满足'static约束的类型提供TypeId,并允许在dyn Any上检查类型或用downcast_ref、downcast_mut取得匹配的具体引用。边界按已读前提解释;示例未执行。
- 原始内容:Any trait instead, because the standard library has additional functionality for working with Any instances (described below). The Any trait has a single method type_id(), which returns the TypeId value for the type that implements the trait. You can’t implement this trait yourself, though, because Any already comes with a blanket implementation for most arb