When I delegate work to threads, I often have a piece of data that survives all threads, for example, numbers in the following example:
use std::thread; fn main() { let numbers = vec![1, 2, 3]; let thread_a = thread::spawn(|| println!("{}", numbers.len())); let thread_b = thread::spawn(|| println!("{}", numbers.len())); thread_a.join().unwrap(); thread_b.join().unwrap(); }
It did not change anywhere, and due to join it ensured that threads were made with it. However, Rust borrow the controller cannot say:
error[E0373]: closure may outlive the current function, but it borrows 'numbers', which is owned by the current function --> src/main.rs:6:34 | 6 | let thread_a = thread::spawn(|| println!("{}", numbers.len())); | ^^ ------- 'numbers' is borrowed here | | | may outlive borrowed value 'numbers' | note: function requires argument type to outlive ''static' --> src/main.rs:6:20 | 6 | let thread_a = thread::spawn(|| println!("{}", numbers.len())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: to force the closure to take ownership of 'numbers' (and any other referenced variables), use the 'move' keyword | 6 | let thread_a = thread::spawn(move || println!("{}", numbers.len())); | ^^^^^^^
All the solutions I've seen so far include cloning a piece of data (or cloning Arc data). Is it possible to do this without cloning?
multithreading rust borrow-checker
Johannes Hoff
source share