I have the struct that looks like:
pub struct MonthlyProjection {
pub sequence: u32,
pub total_deposit: f64,
}
I have this function which takes reference of the vector that contains these structs:
fn generate_projections(simulation_results: &Vec<MonthlySimulationResult>)
Inside this function, I have to group the MonthlyProject structs by their sequences, and do some calculation with it.
So here is my logic:
- Create mutable
HashMap<u32, Vec<MonthlySimulationResult>>, calledresult_map - For loop over the
simulation_resultsvector - If the
result_mapalready has the vector for the given sequence number, then insert the item into that existing vector, then update theresult_map - If the
result_mapdoes not have the existing vector for the given sequence number, then create new vector, insert the struct, and update theresult_map
How would you do this?