知识卡片
永不类型表示没有结果
return、break、panic! 和无出口的 loop 不会向当前求值位置交出正常值,可用 ! 表达这种情况。例如 if 失败分支直接 return,成功分支仍可提供整个 if 的正常结果。函数若标明 -> !,编译器要求它没有任何正常返回路径,std::process::exit 是一个例子。
关联阅读:[[分支需要统一结果类型]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Expressions 可达性分析与从不正常完成的表达式 · 查看原书依据
Expressions that don’t finish normally are assigned the special type !, and they’re exempt from the rules about types having to match.
参考来源
- 位置:《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`
- 结论依据:原文解释了发散分支的类型特例,并用 exit 的签名及永久服务循环说明可声明发散函数。
- 原始内容:Expressions that don’t finish normally are assigned the special type !, and they’re exempt from the rules about types having to match.