知识卡片

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

兜底可绑定或忽略

理解等级:中级

match 的最后一个模式可覆盖全部剩余取值。使用 other 会把该值绑定给分支,比如把骰点作为移动距离。使用 _ 只匹配而不绑定,适用于重新掷骰等无需使用点数的操作。若连操作都没有,_ => () 仍能明确表达无动作。

相关卡片 [[匹配必须覆盖全域]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

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

      Rust also has a pattern we can use when we want a catch-all but don’t want to use the value in the catch-all pattern: _ is a special pattern that matches any value and does not bind to that value. This tells Rust we aren’t going to use the value, so Rust won’t warn us about an unused variable.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第06章 枚举与模式匹配(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c06.xhtml`,块 0116) - 结论依据:原文定义了 _ 的匹配与不绑定规则,并与可读取的 other 分支形成对照。 - 原始内容:Rust also has a pattern we can use when we want a catch-all but don’t want to use the value in the catch-all pattern: _ is a special pattern that matches any value and does not bind to that value. This tells Rust we aren’t going to use the value, so Rust won’t warn us about an unused variable.