知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.12.a.1.g.1.a · 生成模型:GPT-6

代数数据类型的状态数

理解等级:中级

乘积类型同时容纳多项数据,总状态数是各部分可取值数量的乘积。书中的 u8 与 bool 字段组合有 256 × 2 = 512 种状态。和类型一次只取一个变体,总状态数是各变体内部状态数的和。Rust 枚举可以让每个变体携带自己的数据。用 Success(T) 和 Failure(String) 表示结果,就从类型上排除了同一个值同时携带成功与失败两种分支的状态。

相关卡片 [[以返回值表示失败]]、[[映射成功值并保留错误]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • From panic to result: Error handling 成功与失败作为类型分支 · 查看原书依据

      There is another approach, originating largely from the functional programming paradigm. We will focus on a part of that solution, a part that has great support in Rust: algebraic data types (ADTs). Whatever language you currently use, you have some experience with these, because one type of ADT is the product type, which you may know under the name “object” or “struct.” Basically, a product type has a total number of possible “states” equal to the product of its properties.

    参考来源

    - 位置:《Write Powerful Rust Macros》第07章《From panic to result: Error handling》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-7.xhtml#p34) - 结论依据:7.3 用 u8 和 bool 对比乘积型组合数与和类型的互斥选择,随后通过 Either 展示它怎样约束成功和失败。 - 原始内容:There is another approach, originating largely from the functional programming paradigm. We will focus on a part of that solution, a part that has great support in Rust: algebraic data types (ADTs). Whatever language you currently use, you have some experience with these, because one type of ADT is the product type, which you may know under the name “object” or “struct.” Basically, a product type has a total number of possible “states” equal to the product of its properties.