0

我是 Rust 新手,这是我遍历两个 FnvHashMap 并找到差异的代码

impl SaveChanges for Employee{
    type Item = Employee;
    type List = FnvHashMap<(i32, i32), Employee>;
    type Id = (i32, i32);

    fn find_changes(
        old: &FnvHashMap<(i32, i32), Employee>,
        new: FnvHashMap<(i32, i32), Employee>,
    ) -> Changes<Employee, (i32, i32)> {
  let deleted = second
            .iter()
            .filter(|(a, _)| !new.contains_key(a))
            .map(|(&a, _)| a)
            .collect();

for (_, employee1) in &first {
    for (_, employee2) in &second {
        if employee1.emp_id == employee2.emp_id && employee1.lang_id == employee2.lang_id {
            
                    values.push(OldNew {
                        old: employee2,
                        new: employee1,
                    });
        }
    }
}
     let new = first
            .into_iter()
            .filter(|(a, _)| !old.contains_key(a))
            .map(|(_, a)| a)
            .collect();


 
 Changes {
            deleted,
            new,
            values,
        }
}

pub struct Changes<T, I> {
    pub deleted: Vec<I>,
    pub new: Vec<T>,
    pub values: Vec<OldNew<T>>,
}

当我将这些值添加到另一个向量时,Rust 抛出编译错误

values,
expected struct `organization::models::employee_stat::Employee`, found `&organization::models::employee_stat::Employee`

任何帮助将非常感激。谢谢

4

1 回答 1

1

问题是您的deleted变量包含Vec<&T>,而不是该字段的Vec<T>预期。deleted这是因为它是从second.iter()其中迭代哈希表中的键和值的引用获得的。有一个类似的问题values

要解决此问题,您应该into_iter()在迭代哈希图时使用,或者调用clone()转换&TT.

于 2021-03-25T22:15:58.133 回答