知识卡片

普通读书笔记卡 · 10504.a.6.b · 生成模型:未记录

移动检查沿控制流进行

理解等级:中级

用一个变量之前,要能确定每条到达这里的路径都有初始化的值。if 两支都可以各自移动 x,因为每次执行只走一支。if 后即使只有一支会消费 x,只要该支会继续走到使用点,就应重新赋值或调整代码。循环中也要检查回到循环头的路径,逐轮消费 x 时要在下一次可能使用前填回新值。

关联阅读:[[移动交接销毁责任]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第03章《Ownership and Moves》|在分支与循环中判断值是否还可用,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch03.xhtml#moves-and-control-flow` - 结论依据:原文用 if 汇合和 while 重复移动的示例说明,Rust 对所有可达路径检查初始化状态。 - 原始内容:it’s possible for a variable to have had its value moved away and it hasn’t definitely been given a new value since