← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 743.d.6.g · 生成模型:未记录

消费迭代器产生结果

理解等级:中级

迭代器的消费方法主动推进并产生最终结果。sum 求和,collect 根据目标类型汇集元素。以 self 接收迭代器的消费方法会使原绑定不能继续使用,创建迭代器本身也不等于执行这些计算。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第13章 函数式语言特性:迭代器与闭包(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c13.xhtml`,块 0120) - 结论依据:`sum` 持有并反复调用 `next`,累积值并返回结果。 - 原始内容:Methods that call next are called consuming adapters because calling them uses up the iterator. One example is the sum method, which takes ownership of the iterator and iterates through the items by repeatedly calling next, thus consuming the iterator. As it iterates through, it adds each item to a running total and returns the total when iteration is complete.