Why on earth is this code not compiling? cannot move out of X which is behind a shared reference

Why on earth is this code not compiling?

use std::thread;
use std::thread::JoinHandle;

fn main() {
    let mut handles: Vec<JoinHandle<i32>> = Vec::new();

    let jh = thread::spawn(move || {
        1 + 1
    });

    handles.push(jh);

    for h in handles.iter() {
        h.join();
    }
}

JoinHandle consumes itself on join, so you just need to remove that .iter()

(I would also unwrap to propagate panics)

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.