知识卡片
目标类型决定收集方式
collect根据目标类型的FromIterator实现收集元素,同一条数字流既可成为Vec也可成为HashSet,通常需类型标注或turbofish消除歧义。unzip将配对流拆为两个集合,partition按条件分组。目标容器的去重和顺序规则会影响结果,不能把它们当成相同表示。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Iterator Consumers · 查看原书依据
FromIterator trait. The FromIterator trait is implemented for all of the standard library collection types (Vec, HashMap, BTreeSet, etc.), but this ubiquity also means that you often have to use explicit types, because otherwise the compiler can’t figure out whether you’re trying to assemble (say) a Vec<i32> or HashSet<i32>: use std::collections::HashSet; //
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Iterator Consumers》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u050-ch01-080)。
- 结论依据:原文在“FromIterator”附近的定义、例子或代码支撑本卡核心说明:collect根据目标类型的FromIterator实现收集元素,同一条数字流既可成为Vec也可成为HashSet,通常需类型标注或turbofish消除歧义。边界按已读前提解释;示例未执行。
- 原始内容:FromIterator trait. The FromIterator trait is implemented for all of the standard library collection types (Vec, HashMap, BTreeSet, etc.), but this ubiquity also means that you often have to use explicit types, because otherwise the compiler can’t figure out whether you’re trying to assemble (say) a Vec<i32> or HashSet<i32>: use std::collections::HashSet; //