Hashmap with snapshots

I'm looking for a data-structure (or crate containing) that allows you to take a snapshot of the hashmap, then continue mutating the data-structure while the snapshot stays unchanged. I want this so I can iterate over keys/values in a hashmap, possibly inserting/removing entries during iteration.

Does anyone have any suggestions?

You can do this with clone. If you want the snapshot to share internal state with the map you're changing, take a look at the im crate.

fn f(map: &mut HashMap<char, usize>) {
    let snapshot = map.clone();
    // Change map however you want here.
}
3 Likes

If you need something that can create a snapshot without performing a deep clone of the entire map, you can use im::HashMap.

2 Likes

Yeah I should have thought of the im crate. Thanks! :slight_smile: