【发布时间】:2019-04-06 13:06:43
【问题描述】:
我的函数返回一个元组引用的Vec,但我需要一个元组的Vec:
use std::collections::HashSet;
fn main() {
let maxs: HashSet<(usize, usize)> = HashSet::new();
let mins: HashSet<(usize, usize)> = HashSet::new();
let intersection = maxs.intersection(&mins).collect::<Vec<&(usize, usize)>>();
}
我应该如何进行转换?
错误:
19 | maxs.intersection(&mins).collect::<Vec<&(usize, usize)>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected tuple, found reference
|
= note: expected type `std::vec::Vec<(usize, usize)>`
found type `std::vec::Vec<&(usize, usize)>`
我是using a for loop to do the conversion,但我不喜欢它,我认为应该有一种模式惯用的方式:
for t in maxs.intersection(&mins).collect::<Vec<&(usize, usize)>>().iter() {
output.push(**t);
}
【问题讨论】: