← 返回刚才阅读的位置

知识卡片

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

单一模式用简写

理解等级:高级

if let 模式 = 表达式 适用于只关心某个模式的场景,命中时能取出变体数据。其 else 可以处理其余所有情况。若每种业务状态都要独立决定行为,显式 match 的穷尽检查更能暴露漏加分支;if let 不会提醒你新增状态。

相关卡片 [[匹配必须覆盖全域]]、[[匹配同时解包数据]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 枚举与模式匹配 Concise Control Flow with if let · 查看原书依据

      Using if let means less typing, less indentation, and less boilerplate code. However, you lose the exhaustive checking that match enforces. Choosing between match and if let depends on what you’re doing in your particular situation and whether gaining conciseness is an appropriate trade-off for losing exhaustive checking.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第06章 枚举与模式匹配(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c06.xhtml`,块 0132) - 结论依据:原文直接对比简洁性和失去 match 穷尽检查的代价。 - 原始内容:Using if let means less typing, less indentation, and less boilerplate code. However, you lose the exhaustive checking that match enforces. Choosing between match and if let depends on what you’re doing in your particular situation and whether gaining conciseness is an appropriate trade-off for losing exhaustive checking.