1

First, here is my greatly simplified code:

let mut myvec = vec![0; 100];
let mut threads = vec![];

let chunks: Vec<&mut [u8]> = myvec.chunks_mut(10).collect();

for chunk in chunks.into_iter() {
    threads.push(thread::spawn(move || {
        fill_chunk_with_data(chunk);
    }));
}

for thread in threads {
    thread.join().unwrap();
}

write_image(&myvec)?;

Usually chunking a vector works fine but in this case the Rust compiler complains that the lifetime for myvec is not long enough. Indeed it makes sense since i am moving mutable references into threads. I thought i could solve the problem with Rc but Rc offers me an immuatable reference only.

In the end i want to pass it again with an immutable reference (i don't know if this is still a problem if the main problem was solved).

How can i make this work?

nnnmmm
  • 7,964
  • 4
  • 22
  • 41
RustProfi
  • 51
  • 1

0 Answers0