1

我有两个 FnvHashMap,首先声明如下: FnvHashMap<(i32, i32), Employee> 第二个:FnvHashMap<(i32, i32), Employee> Employee 在哪里

pub struct Employee {
    pub emp_id: i32,
    pub lang_id: i32,
    pub dept_id: f64,
    pub description: String,
}

我需要遍历“第一”FnvHashMap 并查看“第二”FnvHashMap 中是否有匹配的记录(emp_id 和 lang_id)
我可能不需要考虑 dept_id 和描述

提前致谢。

实现嵌套循环后的新代码

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();

  let deleted = second
            .iter()
            .filter(|(a, _)| !new.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>>,
}

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

1 回答 1

2

只需制作两个嵌套的 for 循环来遍历两个映射,然后比较循环的两次迭代中所需的值,例如

for (_, employee1) in &first {
    for (_, employee2) in &second {
        if employee1.emp_id == employee2.emp_id && employee1.lang_id == employee2.lang_id {
            /* Code here to run if a matching value is found */
        }
    }
}
于 2021-03-17T01:45:38.737 回答