知识卡片

普通读书笔记卡 · 10643 · 生成模型:未记录

if分支合成一个值

理解等级:中级

if 先检查一个布尔条件,再按所选块的末尾表达式产生结果。写 let value = if flag { 5 } else { 6 }; 时两个分支必须能合为同一结果类型。用 number != 0 显式构造布尔条件,不能直接把整数交给 if。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第03章 常见编程概念(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c03.xhtml`,块 0268) - 结论依据:原文用两个数值分支和数值/字符串冲突展示表达式类型。 - 原始内容:Remember that blocks of code evaluate to the last expression in them, and numbers by themselves are also expressions. In this case, the value of the whole if expression depends on which block of code executes.