知识卡片
永不返回的表达式类型
! 没有可构造的值,对应永不返回到当前求值位置的表达式。一个 match 分支若是 continue,控制流会跳至循环开头,不会给结果变量赋值。这个分支的 ! 能强制转换成另一分支的 u32。类似结构还包括 panic! 和无法退出的循环;普通可能返回 () 的表达式依然要满足分支类型约束。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 高级特性 Advanced Types · 查看原书依据
The formal way of describing this behavior is that expressions of type ! can be coerced into any other type. We’re allowed to end this match arm with continue because continue doesn’t return a value; instead, it moves control back to the top of the loop, so in the Err case, we never assign a value to guess.
参考来源
- 位置:《The Rust Programming Language(第2版)》第19章 高级特性(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c19.xhtml`,块 0262)
- 结论依据:原文说明 ! 可强制转换为其他类型,并解释继续循环时无需为 guess 生成类型不符的值。
- 原始内容:The formal way of describing this behavior is that expressions of type ! can be coerced into any other type. We’re allowed to end this match arm with continue because continue doesn’t return a value; instead, it moves control back to the top of the loop, so in the Err case, we never assign a value to guess.