知识卡片

普通读书笔记卡 · 6074.c.2.a

穷尽匹配检查所有变体

理解等级:中级

Rust的match需要覆盖被匹配类型的所有可能变体,遗漏Teapot等枚举值会被编译器拒绝。显式分支能让新增状态引发检查,通配分支虽满足穷尽性,却可能把新变体悄悄纳入默认处理。应按是否希望未来变化被提醒来选择覆盖方式。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types enums · 查看原书依据

      consider all of the possibilities that are represented by the enum,3 even if the result is just to add a default arm _ => {}. (Note that modern C++ compilers can and do warn about missing switch arms for enums as well.)

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《enums》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u010-ch01-007)。 - 结论依据:原文在“consider all of the possibilities”附近的定义、例子或代码支撑本卡核心说明:Rust的match需要覆盖被匹配类型的所有可能变体,遗漏Teapot等枚举值会被编译器拒绝。边界按已读前提解释;示例未执行。 - 原始内容:consider all of the possibilities that are represented by the enum,3 even if the result is just to add a default arm _ => {}. (Note that modern C++ compilers can and do warn about missing switch arms for enums as well.)