知识卡片
空集合不等于未提供
零元素与没有提供集合并不总是同一含义。若业务需要区分,应使用Option>:None表示未提供,Some(vec![])表示明确提供空集合。字符串也一样,空串可以是实际值。是否保留两层状态取决于业务契约,不应仅为简短把它们合并。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Option<T> · 查看原书依据
not having a collection. For most situations, the distinction doesn’t arise and you can go ahead and use (say) Vec<Thing>: a count of zero things implies an absence of things. However, there are definitely other rare scenarios where the two cases need to be distinguished with Option<Vec<Thing>>—for example, a cryptographic system might need to distinguish be
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Option<T>》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u013-ch01-010)。
- 结论依据:原文在“not having a collection”附近的定义、例子或代码支撑本卡核心说明:零元素与没有提供集合并不总是同一含义。边界按已读前提解释;示例未执行。
- 原始内容:not having a collection. For most situations, the distinction doesn’t arise and you can go ahead and use (say) Vec<Thing>: a count of zero things implies an absence of things. However, there are definitely other rare scenarios where the two cases need to be distinguished with Option<Vec<Thing>>—for example, a cryptographic system might need to distinguish be