Rust HashMap find and remove minimum value

67 Views Asked by At

I have a HashMap<(usize,usize),Point3d> and I want to remove the minimum element according to some metric.

        let closest_edge = {
            let edges = exterior.keys();
            edges.reduce(|k1:&(usize,usize),k2:&(usize,usize)|-> &(usize,usize) {
                // return minimal value
            }).unwrap()
        };

        exterior.remove(closest_edge);

However, I cannot remove the minimal edge because of a borrow as immutable in getting the keys from exterior. Specifically this is my error:

cannot borrow `exterior` as mutable because it is also borrowed as immutable

Is there an idiomatic way to write a queue that reshuffles?

Queue doesn't work because I want the minimal element, not the oldest element.

retain also doesn't work because I would need to know what the minimal distance is before hand, and I want to only remove one element.

PriorityQueue is also not quite right, since the priority may change from round to round. (Each round I want to find an edge that is closest to the point I'm looking at, so the distance function changes each time)

1

There are 1 best solutions below

0
drewtato On BEST ANSWER

Since your key is Copy, you can copy it by dereferencing so it isn't borrowing from the map.

let closest_edge = {
    let edges = exterior.keys();
    // Dereference *
    *edges.reduce(|k1:&(usize,usize),k2:&(usize,usize)|-> &(usize,usize) {
        // return minimal value
    }).unwrap()
};

// Make temporary reference &
exterior.remove(&closest_edge);