知识卡片

操作参考卡 · 743.d.6.d

筛选与展开需要保留错误意图

理解等级:中级

filter逐项保留满足条件的值,take_while只保留开头连续满足条件的一段,skip_while跳过开头满足条件的一段。flatten展开内部可迭代值;Option或Result各最多产出一个成功值,因此flatten会丢弃None或Err。若需遇错失败,应保留Result并选择传播机制,不能把展开误当错误处理。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types Iterator Transforms · 查看原书依据

      flatten() method deals with an iterator whose items are themselves iterators, flattening the result. On its own, this doesn’t seem that helpful, but it becomes much more useful when combined with the observation that both Option and Result act as iterators: they produce either zero (for None, Err(e)) or one (for Some(v), Ok(v)) items. This means that flatten

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Iterator Transforms》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u049-ch01-076)。 - 结论依据:原文在“flatten()”附近的定义、例子或代码支撑本卡核心说明:filter逐项保留满足条件的值,take_while只保留开头连续满足条件的一段,skip_while跳过开头满足条件的一段。边界按已读前提解释;示例未执行。 - 原始内容:flatten() method deals with an iterator whose items are themselves iterators, flattening the result. On its own, this doesn’t seem that helpful, but it becomes much more useful when combined with the observation that both Option and Result act as iterators: they produce either zero (for None, Err(e)) or one (for Some(v), Ok(v)) items. This means that flatten