← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.b.2.a · 生成模型:未记录

比较结果交给枚举

理解等级:初级

cmp 返回 Ordering,把比较分支表成 Less、Equal 和 Greater。用 match 对三种结果分别安排操作,可把比较的顺序规则与游戏提示分开。示例要求两边先成为可比较的整数。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 猜数字游戏 Comparing the Guess to the Secret Number · 查看原书依据

      Then we add five new lines at the bottom that use the Ordering type. The cmp method ❸ compares two values and can be called on anything that can be compared. It takes a reference to whatever you want to compare with: here it’s comparing guess to secret_number. Then it returns a variant of the Ordering enum we brought into scope with the use statement.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第02章 猜数字游戏(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c02.xhtml`,块 0124) - 结论依据:原文用 cmp 返回的 Ordering 值选择三种提示分支。 - 原始内容:Then we add five new lines at the bottom that use the Ordering type. The cmp method ❸ compares two values and can be called on anything that can be compared. It takes a reference to whatever you want to compare with: here it’s comparing guess to secret_number. Then it returns a variant of the Ordering enum we brought into scope with the use statement.