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.