Spawner.spawn error

So the problem:

 threads.push(spawner.spawn(move || {
     |                              ^^^^^ ------- takes 0 arguments
     |                              |
     |                              expected closure that takes 1 argument

This is strange, because there wasn't error earlier. And as i know- spawn doesn't require arguments.

What's the type of spawner / from which crate does it come from?

crossbeam::scope(|spawner| {
//...
threads.push(spawner.spawn(move || {  
//...

Also if i add |_| here, will be:

error[E0521]: borrowed data escapes outside of closure
    --> src\main.rs:1539:9
     |
1532 |   let mut threads = Vec::with_capacity(10);
     |       ----------- `threads` declared here, outside of the closure body
1533 |   let files_vec = Arc::clone(&files_vec);
1534 |   crossbeam::scope(|spawner| {//vec_of_processes =
     |                     ------- `spawner` is a reference that is only valid in the closure body
...
1539 | /         threads.push(spawner.spawn(move |_| {
//....   
         }
    }));//$Close threads!
     | |_______^ `spawner` escapes the closure body here

Yes, the .spawn from crossbeam requires a |_| closure.


The second error message is coherent with what a crossbeam::scope is: it defines a scope (the closure's block body given to crossbeam::scope()) wherein the threads and thread handles are well defined, but outside of which they have no meaning whatsoever, since the threads will have been auto-joined by then.

That is, the very point of using crossbeam::scope is not having to deal with joining the threads, not only for the caller's convenience, but also because this auto-join property is then known by the compiler, which will then allow you to borrow outer locals inside it :slightly_smiling_face:

So, I'd suggest to simply get rid of your threads vec altogether

1 Like

Thanks much, i undertood why there wasn't error- i had changed crossbeam version)

1 Like