3 ms·
I'm not sure what your performance constraints are, but you could try using RefCell<T> for objects which need to be shared with the closure. It's a fairly low o
by slaymaker1907 4y ago
I'm not sure what your performance constraints are, but you could try using RefCell<T> for objects which need to be shared with the closure. It's a fairly low overhead abstraction. Also, you can try creating a reference explicitly that gets used in the closure for objects which don't need mut references.
let helperX = &x;
let ycell = RefCell::new(y);
let yref = &ycell;
fn helper() {
helperX.whatever();
ycell.borrow_mut().ystuff();
}
In this case, RefCell checks at runtime that you don't have an outstanding borrow in your regular code (which you can avoid via explicit drops or by carefully using blocks). It's essentially a very low overhead RW lock and unlike Rc, it doesn't even allocate on the heap.