Let's analyze a function which maintains some 3×1 f64 vectors for plotting. There are multiple vectors and each of them is behind an enum key of a HashMap. HashMap is a member property of a struct, so it looks something like this:

struct Particle {
    data_trace: HashMap<ParticleProperty, Vec<[f64; 3]>>,
}

The Problem

We have a function log_debug_data which will record the current position of a particle into the data_trace HashMap for the key ParticleProperty::Position:

fn log_debug_data(&mut self, position: [f64; 3]) {
    match self.data_trace.get_mut(&ParticleProperty::Position) {
        Some(trace) => trace.push(position),
        None => {
            self.data_trace.insert(ParticleProperty::Position, vec![position]);
        }
    }
}

We use pattern matching to check if the key already exists in the map so we know if a new vector should be created with a single entry or an element should be appended to an existing vector.

The Performance Issue

As you can see, appending the element to an existing vector had to be done through cloning the vector first, to obtain the mutable reference to it. If we tried to get the mutable reference without the cloning, Rust would complain about data_trace being of type Vec<>, which doesn't implement the Copy trait needed for generating mutable reference.

Even if somehow we managed to make the compiler happy, the fact we got the mutable reference of something immutable must have meant Rust copied it under the hood, which is something we were aiming to avoid in the first place. So, what can we do?

The Solution: RefCell

To avoid copying, we must change our struct so the data_trace property's vectors are each wrapped with a RefCell<>:

struct Particle {
    data_trace: HashMap<ParticleProperty, RefCell<Vec<[f64; 3]>>>,
}

RefCell Solution

RefCell acts as an immutable wrapper to a value it holds, which is of type &T. Using its .borrow_mut() or .get_mut() methods you can mutate the value it wraps. It is a great way of having multiple mutable references at the same time (if you are operating in a single-threaded environment).

So now instead of cloning the whole vector, we will simply get a mutable reference to it and append the element in place, which will truly get rid of the expensive clone operation and is still guaranteed to be safe. My first attempt looked like this:

fn log_debug_data(&mut self, position: [f64; 3]) {
    match self.data_trace.get_mut(&ParticleProperty::Position) {
        Some(trace) => trace.get_mut().push(position),
        None => {
            self.data_trace.insert(
                ParticleProperty::Position,
                RefCell::new(vec![position])
            );
        }
    }
}

If you look closely, I ended up getting not just a mutable reference to the underlying vector I want to mutate, but to the parent RefCell as well. Although this works, there's no need to do that. Switching from HashMap's mutable data_trace.get_mut() to immutable data_trace.get() forced me to change the way of getting the internal vector behind the RefCell from trace.get_mut() to trace.borrow_mut(). Based on the documentation for get_mut(), this seems to be a very good thing. The final code looks like this:

fn log_debug_data(&self, position: [f64; 3]) {
    match self.data_trace.get(&ParticleProperty::Position) {
        Some(trace) => trace.borrow_mut().push(position),
        None => {
            self.data_trace.insert(
                ParticleProperty::Position,
                RefCell::new(vec![position])
            );
        }
    }
}

This approach avoids the expensive clone operation entirely while maintaining Rust's memory safety guarantees at runtime through the borrow checker's runtime checks in RefCell.