知识卡片

普通读书笔记卡 · 10880.f.1 · 生成模型:未记录

数值转换按类型决定结果

理解等级:中级

用 as 把整数缩窄时保留目标位宽的低位,扩宽时按源类型符号扩展或补零。浮点转整数先向零取整,再把超出范围的值限制在目标边界。例如 -1.99 as i32 为 -1,1e6 as u8 为 255。向 char 转换时只有 u8 的全部取值都被允许直接转换,其他整数可在适当范围内用 char::from_u32 检查。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第05章《Expressions》|区分显式数值转换和引用强制转换,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch05.xhtml#type-casts` - 结论依据:原文分别解释整数位宽变换、浮点截断和饱和、Unicode 代理码位为何需要检查。 - 原始内容:If the value is too large to fit in the integer type, the cast produces the closest value that the integer type can represent: