1

I have two FnvHashMap which is declared like first: FnvHashMap<(i32, i32), Employee> second: FnvHashMap<(i32, i32), Employee> Where Employee is

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
I may not need to consider dept_id and description

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`
Ann
  • 31
  • 1
  • 6
  • So you want to compare the values of both hashmaps and ignore all the keys? – curiousdannii Mar 17 '21 at 01:09
  • yes ,basically I am comparing two tables with similar structure and looking for changes but I have two keys (composite primary keys) in order to match the records . – Ann Mar 18 '21 at 17:43

1 Answers1

2

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 (_, 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 */
        }
    }
}
Colby Quinn
  • 138
  • 2
  • 8
  • Thank you for the code .I am new to Rust . Just wondering Is there any other way to reduce the complexity ? my hashMap has quite large number of records ,nested loop might increase the complexity. – Ann Mar 17 '21 at 22:03
  • @Ann not to my knowledge, your going to have to iterate through everything to find the values because you will not know where they are. You could collect all of the values from the Maps into Vectors and then sort them by employee id and do some sort of searching algorithm to find the values, this could be faster if you have a very large dataset but would make the implementation much more complex. – Colby Quinn Mar 18 '21 at 11:57
  • 1
    Thanks Colby . I will look into that . – Ann Mar 18 '21 at 17:20
  • Could you help me to solve this error after implementing the loop ```expected struct `organization::models::employee_stat::Employee`, found `&organization::models::employee_stat::Employee` ``` I have added my code in the description . – Ann Mar 18 '21 at 17:49
  • @Ann I don't know where this error is occurring because you did not show in the error message, but the error is just that you need to dereference the Employee using a `*` like ``*EmployeeHere``. Also you should make a new question for this because it is a new question. – Colby Quinn Mar 23 '21 at 15:18
  • Hi Colby ,here is the link to my new post as you suggested . https://stackoverflow.com/questions/66804783/how-to-resolve-compilation-error-while-borrowing-values-in-rust – Ann Mar 25 '21 at 17:37