知识卡片
模式匹配须穷尽取值
match 以源码顺序测试模式,只执行首个匹配的分支,也要求每种值至少能命中一支。用 Some(name) 和 None 可以完整覆盖 Option;匹配错误码时可用最后一个 _ 兜底。把 _ 提到具体错误码之前,会让这些具体分支再无机会执行。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Expressions 统一分支结果并确保 match 穷尽 · 查看原书依据
When a pattern matches, the corresponding expr is evaluated, and the match expression is complete; no further patterns are checked.
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第05章《Expressions》|统一分支结果并确保 match 穷尽,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch05.xhtml#if-and-match`
- 结论依据:原文对照有通配符的错误码分派与遗漏部分牌面值的非穷尽匹配。
- 原始内容:When a pattern matches, the corresponding expr is evaluated, and the match expression is complete; no further patterns are checked.