知识卡片

普通读书笔记卡 · 743.d.6

容器变换表达处理意图

理解等级:中级

map在成功值或Some值上做普通转换;and_then接收返回同类容器的操作并避免额外嵌套。Result的map_err只转换错误,Option的ok_or或ok_or_else可把缺失转换成具体失败。选函数应看想保留哪种状态;若match更容易说明多分支业务,就保留它。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Item 3: Prefer Option and Result transforms over explicit match expressions》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u022-ch01-026)。 - 结论依据:原文在“transform”附近的定义、例子或代码支撑本卡核心说明:map在成功值或Some值上做普通转换;and_then接收返回同类容器的操作并避免额外嵌套。边界按已读前提解释;示例未执行。 - 原始内容:transforms over explicit match expressions Item 1 expounded the virtues of enum and showed how match expressions force the programmer to take all possibilities into account. Item 1 also introduced the two ubiquitous enums that the Rust standard library provides: Option<T> To express that a value (of type T) may or may not be present Result<T, E> For when an