Let's create a test to establish the best way to share mutable ownership in just a few steps:
- Let's create a couple of new functions inside the testing module:
use std::cell::{Cell, RefCell}; use std::borrow::Cow; use std::ptr::eq; fn min_sum_cow(min: i32, v: &mut Cow<[i32]>) { let sum: i32 = v.iter().sum(); if sum < min { v.to_mut().push(min - sum); } } fn min_sum_refcell(min: i32, v: &RefCell<Vec<i32>>) { let sum: i32 = v.borrow().iter().sum(); if sum < min { v.borrow_mut().push(min - sum); } } fn min_sum_cell(min: i32, v: &Cell<Vec<i32>>) { let mut vec = v.take(); let sum: i32 = vec.iter().sum(); if sum < min { vec.push(min - sum); } v.set(vec); }
- These functions dynamically (based on incoming data) mutate a list of ...