0

I am new to Rust and here is my code to iterate through two FnvHashMaps and find the difference

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 throwing compilation error when I add those values to another vector

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

Any help would be really appreciated. Thanks

Ann
  • 31
  • 1
  • 6
  • Which line do you get the error on? And can you please provide some more code, for context, like the definitions and types of `values` and `first` and `second`? It should be fairly easy to fix by modifying it to expect a reference, or instead clone the data, but it's hard to give a more specific answer without seeing more context. A runnable example would allow me to test it on my machine – transistor Mar 25 '21 at 19:32
  • The error for the code you have provided is: `error: expected item, found keyword 'for'`. See https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=71020981ff8d7466cdf109a5b2d65264 – Peter Hall Mar 25 '21 at 19:55
  • But, given we have to guess, I'll guess that you need to add a `&` or a `*` somewhere around those `==` expressions. – Peter Hall Mar 25 '21 at 19:57
  • I am getting error for 'values' in ``` Changes { deleted, new, values, } ``` Error is : expected struct `organization::models::employee_stat::Employee`, found `&organization::models::employee_stat::Employee` I could see the expected result in 'values' but when I assign that value array to 'Changes' ,it throwing that reference error. I am not sure where I suppose to clone() or add * – Ann Mar 25 '21 at 21:02
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Apr 05 '21 at 20:28
  • The code you have provided [is not syntactically valid Rust](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a56edb64e5e8b5eb05d6fc1991843d21). Making guesses as to what it might be leads to a number of missing types and traits. Try following the MRE instructions above to produce a self-contained example. – Shepmaster Apr 05 '21 at 20:28

1 Answers1

1

The problem is that your deleted variable contains Vec<&T>, not the Vec<T> expected by the deleted field. This is because it is obtained from second.iter() which iterates over references to keys and values in the hash table. There is a similar issue with values.

To fix this, you should either use into_iter() when iterating over the hashmaps, or call clone() to convert &T to T.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • Could you point out where I should add clone() in my code. I tried but still getting the error. – Ann Apr 01 '21 at 23:05
  • @Ann For example, change `.map(|(&a, _)| a)` to `.map(|(&a, _)| a.clone())` when building `deleted`. More might be needed, but it's hard to tell without a reproducible example. – user4815162342 Apr 02 '21 at 03:47
  • I am still getting error in values. I am not sure where to add clone() or & in this piece of code ? .Sorry I donno how to provide the debugging logs for you other than the error code I mentioned in description. ``` 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, }); } } } ``` – Ann Apr 05 '21 at 20:11
  • @Ann As I said, we can't help you without a reproducible example, i.e. a self-sufficient module that includes the definition of `Employee` and other classes defined by you, and that shows the issue you are having. You can edit your question to provide additional information, code snippets and answers don't fit in comments. – user4815162342 Apr 05 '21 at 21:47