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.

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


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

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.