知识卡片
无条件循环传达可达性
控制流分析负责检查缺少返回、变量尚未初始化、语句不可达等问题。while true 虽然字面上永远进入循环,while 仍走条件循环的类型规则,不能用作要求 i32 的函数返回值。改用 loop 表明入口无条件;若只有内部 return 或永远迭代,末尾无需提供额外的普通返回值。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Expressions 可达性分析与从不正常完成的表达式 · 查看原书依据
The loop expression is offered as a “say-what-you-mean” solution to this problem.
参考来源
- 位置:《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#why-rust-has-loop`
- 结论依据:原文的等待进程示例由于 while 的 () 与 i32 不匹配而报错,并用 loop 明确不经条件退出。
- 原始内容:The loop expression is offered as a “say-what-you-mean” solution to this problem.