我有以下代码,其中facreturn (MyType, OtherType):
let l = (-1..13).map(|x| {
fac(x).0
}).collect::<Vec<MyType>>();
Run Code Online (Sandbox Code Playgroud)
它有效,但我正在丢弃这些OtherType值。所以我决定使用.unzip,像这样:
let (v, r) = (-1..13).map(|x| {
fac(x)
}).unzip();
let l = v.collect::<Vec<MyType>>();
let q = r.collect::<Vec<OtherType>>();
Run Code Online (Sandbox Code Playgroud)
但是类型推断失败了:
error: the type of this value must be known in this context
let l = v.collect::<Vec<Literal>>();
^~~~~~~~~~~~~~~~~~~~~~~~~~~
let q = r.collect::<Vec<OtherType>>();
^~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
问题是:我不知道也不关心迭代器的具体类型是什么(我认为编译器可以推断它们,如第一个片段所示)。在这种情况下如何满足编译器?
另外,我宁愿重组的代码-我不喜欢单独通话.collect()双方v和r。理想情况下,我会在 之后继续方法链.unzip(),Vec在该表达式中返回两个s。
.unzip()不返回迭代器——它就像两个并行收集!实际上,您可以将这两部分收集到不同类型的集合中,但让我们在此示例中对两者都使用向量:
// Give a type hint to determine the collection type
let (v, r): (Vec<MyType>, Vec<OtherType>) = (-1..13).map(|x| {
fac(x)
}).unzip();
Run Code Online (Sandbox Code Playgroud)
这样做是为了尽可能简单和透明。返回两个迭代器需要它们共享一个公共状态,这是 rust 的迭代器库倾向于避免的复杂性。
| 归档时间: |
|
| 查看次数: |
1246 次 |
| 最近记录: |