Hello,
I found myself running into the following problem several times: I'd like to mutate an attribute within a method, using a closure which calls a second method. Consider the following example.
struct Adder {
value: f64,
some_vector: Vec<f64>,
}
impl Adder {
fn add(&self, x: f64) -> f64 {
x + self.value
}
fn modify_attribute(&mut self) {
let f = |x: f64| self.add(x);
self.some_vector.iter_mut().for_each(|x| *x = f(*x));
}
}
The method modify_attribute
uses the closure
f = |x: f64| self.add(x)
borrowing self
immutably. However, then self.some_vector
is borrowed mutably, which is not possible. In some cases, it is possible to get around the issue by defining the function outside of the structure, and cloning the necessary parameters.
impl Adder {
fn modify_attribute(&mut self) {
let value = self.value.clone();
let f = |x: f64| add(value, x);
self.some_vector.iter_mut().for_each(|x| *x = f(*x));
}
}
fn add(value: f64, x: f64) -> f64 {
value + x
}
However this is not always possible (when cloning is expensive), and I'd like to keep the logic of the function within my structure if possible. Is there a standard way to solve this problem ?