Can I use a map function on a hashmap using a function, not a closure?

I have a hashmap that contains a struct as the value, hashmap<String, Struct>. The struct contains an implementation that has several functions.

#[derive(Debug)]
pub struct Rand_val{
    first: f64,
    second: f64,
}

impl Rand_val{

    pub fn new(vector: Vec<f64>) -> Rand_val{

        Rand_val{ 
            first: vector[0],
            second: vector[1], 
        }
    }

    pub fn update_value(&mut self, num: f64) {

        self.first = self.first + num;
        self.second = self.second + num;
    }
}

This is not my actual code just an example.

I'm my main I want to iterate through the hashmap and perform the update_value call at random times.

Now I can do this easily by creating a loop

let mut map_data = Rand_val::new(vec![5.1, 4.2])

for (_, val) in map_data.iter_mut() {
      val.update_value(4.5);
}

this works exactly as expected. It takes the new value and updates the struct variables with the passed parameter.

What I would like to do is learn a little more about the map function. I would like to do the same thing, but using the map function instead.

map_data.iter_mut().map(|data| data.1.update_value(7.8));

this builds just fine. Runs with no issues, but the values in my struct never get updated. I added a print statement to the update_value function and it doesn't print anything. So I can assume it's not calling the function correctly.

Could someone provide me with some insight as to 1) why its not working 2) if this is even possible?

Thank you.

Note that map is lazy, just creating a new Map iterator without executing anything yet. You should be getting a compiler must_use warning about that. You probably want for_each instead.

@cuviper you are right, I am getting a warning that the map is unused.

Thank you so much. I replaced map() with for_each(). and now it works as expected.

Can you elaborate on what you mean by the map is lazy? I thought the implementation was within the map. Why didn't it execute?

The call to map just stores the input iterator and the mapping function into a new Map iterator. It doesn't actually execute your code at that time. Then, each time that Map::next() is called (by any means of using the iterator), it will call the inner iterator's next(), pass that value through your mapping function, and yield the return value of that function as the map's iterator item. This is called "lazy" because it doesn't do any work until specifically required, on-demand.

Your function doesn't return anything, which is implicitly the same as -> (), so that's how I knew you were not intending to have a Map returning new items.

@cuviper Thank you so much for clarifying that. It makes perfect sense to me now. Thank you!!!!