简体   繁体   English

如何在 Rust 中迭代 FnvHashMap

[英]How to iterate through FnvHashMap in Rust

I have two FnvHashMap which is declared like first: FnvHashMap<(i32, i32), Employee> second: FnvHashMap<(i32, i32), Employee> Where Employee is我有两个 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,
}

I need to iterate through 'first' FnvHashMap and see if there is a matching record(emp_id and lang_id) in 'second' FnvHashMap我需要遍历“第一个”FnvHashMap 并查看“第二个”FnvHashMap 中是否有匹配的记录(emp_id 和 lang_id)
I may not need to consider dept_id and description我可能不需要考虑 dept_id 和描述

Thanks in Advance.提前致谢。

New code after implementing nested loop实现嵌套循环后的新代码

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`

Simply make two nested for loops to iterate through both maps and then compare the values you need from the two iterations of the loops, for example只需制作两个嵌套的 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 */
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM