知识卡片
显式引用转换可有多个目标
AsRef与AsMut通过显式方法提供廉价引用转换,不触发Deref那样的自动解引用行为。目标T是泛型参数,一个类型可支持多个目标视图,例如String可转为str、字节或路径引用。选择应看调用者需要的视图,不把多个视图都强行设成解引用目标。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Pointer Traits · 查看原书依据
AsRef and AsMut traits. These traits don’t induce special behavior in the compiler but allow conversions to a reference or mutable reference via an explicit call to their trait functions (as_ref() and as_mut(), respectively). The destination type for these conversions is encoded as a type parameter (e.g., AsRef<Point>), which means that a single container ty
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Pointer Traits》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u041-ch01-056)。
- 结论依据:原文在“AsRef and AsMut”附近的定义、例子或代码支撑本卡核心说明:AsRef<T>与AsMut<T>通过显式方法提供廉价引用转换,不触发Deref那样的自动解引用行为。边界按已读前提解释;示例未执行。
- 原始内容:AsRef and AsMut traits. These traits don’t induce special behavior in the compiler but allow conversions to a reference or mutable reference via an explicit call to their trait functions (as_ref() and as_mut(), respectively). The destination type for these conversions is encoded as a type parameter (e.g., AsRef<Point>), which means that a single container ty