Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

rustlings: hashmap3.rs expected `()`, found struct `Team`

I’m learning rust using rustlings. I was trying to implement a solution in a functional way (rustlings only asked for the imperative way). I’m running into the following compiler error and haven’t been able to figure this out since 2 hours. Please help

Compiler error:

Progress: [############################>-------------------------------] 45/94
! Compiling of exercises/hashmaps/hashmaps3.rs failed! Please try again. Here's the output:
error[E0308]: mismatched types
  --> exercises/hashmaps/hashmaps3.rs:94:28
   |
94 |           .and_modify(|team| Team {
   |  ____________________________^
95 | |             name: team.name.to_string(),
96 | |             goals_scored: team.goals_scored+team_from_iter.goals_scored,
97 | |             goals_conceded: team.goals_conceded+team_from_iter.goals_conceded,
98 | |         })
   | |_________^ expected `()`, found struct `Team`       

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`. 

Code

This is the set of lines the compiler has an issue with

.and_modify(|team| Team {
    name: team.name.to_string(),
    goals_scored: team.goals_scored+team_from_iter.goals_scored,
    goals_conceded: team.goals_conceded+team_from_iter.goals_conceded,
})

Link to full code repo:

  1. To the exact lines with the issue: https://github.com/dheerajbhaskar/learn-rust-rustlings-PUBLIC/blob/a3d38c643fc178f05a847e7cb8016a784b0a54ac/exercises/hashmaps/hashmaps3.rs
  2. To the repo: https://github.com/dheerajbhaskar/learn-rust-rustlings-PUBLIC

Additional information

  1. I’ve written very similar code on line 119 which compiles and passes the tests. I’ve not been able to understand why that worked but code at line 94 above didn’t. Edit: looks like I’ve compiler error both these lines now ¯\_(ツ)_/¯
  2. I’m a rust noob if it wasn’t apparent yet 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

pub fn and_modify<F>(self, f: F) -> Self
where
    F: FnOnce(&mut V),

and_modify‘s argument has the type FnOnce(&mut V). This is a function that takes a single argument of type &mut V and returns nothing. (If it were returning something it would have a signature like FnOnce(&V) -> V instead.)

Concretely, you’re receiving a mutable Team object that’s already in the hash map and you’re expected to modify it in place. Instead of constructing a brand new Team you should update the team that you’re being passed. Something like this, say:

.and_modify(|team| {
    team.goals_scored += team_from_iter.goals_scored;
    team.goals_conceded += team_from_iter.goals_conceded;
})

Notice how this modifies team in place and doesn’t return anything.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading