Another solution for this case is to move a mutable reference to the item into the thread, even though mutability isn’t required. Since there can be only one mutable reference, the compiler knows that it’s safe to be used in another thread.
use crossbeam; // 0.7.3
use std::cell::RefCell;
fn main() {
let mut val = RefCell::new(1);
let val2 = &mut val;
crossbeam::scope(|scope| {
scope.spawn(move |_| *val2.borrow());
})
.unwrap();
}
As bluss points out:
This is allowed because
RefCell<i32>
implementsSend
.