← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.d.6.d.1 · 生成模型:未记录

带值跳出指定循环

理解等级:中级

loop 的结果可用 break result 提供。若在嵌套循环里查找目标,可以用 break ‘outer result 从指定的外层 loop 返回结果。所有向同一个 loop 提交的 break 值应有统一类型;for 和 while 只能用 break 结束,continue 则跳到目标循环的下一轮。

关联阅读:[[标签块可以提前求值]]。

这张卡片看不懂?

已有反馈 · 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#break-and-continue-expressions` - 结论依据:原文展示搜索首个答案时以 break 给 loop 赋值,以及在内层 for 得到平方根后向外层 loop 交付结果。 - 原始内容:All the break expressions within a loop must produce values with the same type, which becomes the type of the loop itself.