知识卡片

普通读书笔记卡 · 6074.a.2

区分三种类型转换

理解等级:中级

Rust类型转换有不同通道:From/Into等trait表达自定义转换,as执行语言支持的强制转换,coercion在特定位置自动调整类型。u32扩大为u64也不会仅因安全就隐式赋值成功。先辨认转换通道,再判断其失败、信息丢失与调用边界。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types Item 5: Understand type conversions · 查看原书依据

      Manual User-defined type conversions provided by implementing the From and Into traits Semi-automatic Explicit casts between values using the as keyword Automatic Implicit coercion into a new type The majority of this Item focuses on the first of these, manual conversions of types, because the latter two mostly don’t apply to conversions of user-defined type

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Item 5: Understand type conversions》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u031-ch01-038)。 - 结论依据:原文在“Manual”附近的定义、例子或代码支撑本卡核心说明:Rust类型转换有不同通道:From/Into等trait表达自定义转换,as执行语言支持的强制转换,coercion在特定位置自动调整类型。边界按已读前提解释;示例未执行。 - 原始内容:Manual User-defined type conversions provided by implementing the From and Into traits Semi-automatic Explicit casts between values using the as keyword Automatic Implicit coercion into a new type The majority of this Item focuses on the first of these, manual conversions of types, because the latter two mostly don’t apply to conversions of user-defined type