知识卡片

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

匹配必须覆盖全域

理解等级:中级

match 要为类型的每个可能值提供分支,编译器能指出遗漏的 None 等模式。有限业务状态可以逐一列举分支,新增变体后编译错误能提醒重新决定业务行为。若多个值本来就共享一种默认行为,最后的通配模式可集中处理。

相关卡片 [[首个匹配分支产值]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 枚举与模式匹配 Matches Are Exhaustive · 查看原书依据

      error[E0004]: non-exhaustive patterns: `None` not covered --> src/main.rs:3:15 | 3 | match x { | ^ pattern `None` not covered | note: `Option<i32>` defined here = note: the matched value is of type `Option<i32>` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | 4 ~ Some(i) => Some(i + 1), 5 ~ None =>

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第06章 枚举与模式匹配(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c06.xhtml`,块 0109) - 结论依据:E0004 诊断定位到未覆盖的 None,并展示怎样满足穷尽性。 - 原始内容:error[E0004]: non-exhaustive patterns: `None` not covered --> src/main.rs:3:15 | 3 | match x { | ^ pattern `None` not covered | note: `Option<i32>` defined here = note: the matched value is of type `Option<i32>` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | 4 ~ Some(i) => Some(i + 1), 5 ~ None =>