title: Unable to join threads from JoinHandles stored in a Vector - Rust
body: <p>I am writing a program which scrapes data from a list of websites and stores it into a struct called <code>Listing</code> which is then collected into a final struct called <code>Listings</code>.</p>
  <pre><code>use std::{ thread,
             sync::{ Arc, Mutex }
           };
  
  fn main() {
      // ... some declarations
      let sites_count = site_list.len(); // site_list is a vector containing the list of websites
  
      // The variable to be updated by the thread instances ( `Listing` is a struct holding the information ) 
      let listings: Arc&lt;Mutex&lt;Vec&lt;Vec&lt;types::Listing&lt;String&gt;&gt;&gt;&gt;&gt; = Arc::new(Mutex::new(Vec::new()));
  
      // A vector containing all the JoinHandles for the spawned threads
      let mut fetch_handle: Vec&lt;thread::JoinHandle&lt;()&gt;&gt; = Vec::new();
  
      // Spawn a thread for each concurrent website
      for i in 0..sites_count { 
          let slist = Arc::clone(&amp;site_list);
          let listng = Arc::clone(&amp;listings);
          fetch_handle.push(
              thread::spawn(move || {
                  println!(&quot;⌛ Spawned Thread: {}&quot;,i);
                  let site_profile = read_profile(&amp;slist[i]);
                  let results = function1(function(2)) // A long list of functions from a submodule that make the http request and parse the data into `Listing`
                  listng.lock().unwrap().push(results);
              }));
      }
      
      for thread in fetch_handle.iter_mut() { 
          thread.join().unwrap();
      }
  
      // This is the one line version of the above for loop - yields the same error.
      // fetch_handle.iter().map(|thread| thread.join().unwrap()); 
  
      // The final println to just test feed the target struct `Listings` with the values
      println!(&quot;{}&quot;,types::Listings{ date_time: format!(&quot;{}&quot;, chrono::offset::Local::now()),
                                     category: category.to_string(),
                                     query: (&amp;search_query).to_string(),
                                     listings: listings.lock().unwrap() // It prevents me from owning this variable
                                   }.to_json());
  }
  
  </code></pre>
  <p>To which I stumble upon the error</p>
  <pre><code>error[E0507]: cannot move out of `*thread` which is behind a mutable reference
     --&gt; src/main.rs:112:9
      |
  112 |         thread.join().unwrap();
      |         ^^^^^^ move occurs because `*thread` has type `JoinHandle&lt;()&gt;`, which does not implement the `Copy` trait
  </code></pre>
  <p>It prevents me from owning the variable after the thread.join() for loop.</p>
  <p>When I tried assigning to check the output type</p>
  <pre><code>let all_listings = listings.lock().unwrap()
  </code></pre>
  <p><code>all_listings</code> reports a type of MutexGuard(which is also true inside the thread for loop, but it allows me to call vector methods on it) and wouldn't allow me to own the data.
  I changed the data type in the <code>Listings</code> struct to hold a reference instead of owning it. But it seems so the operations I perform on the struct in <code>.to_json()</code> require me to own its value.
  The type declaration for <code>listings</code> inside the <code>Listings</code> Struct is <code>Vec&lt;Vec&lt;Listing&lt;T&gt;&gt;</code>.</p>
  <p>This code however works just fine when I move the <code>.join().unwrap()</code> to the end of <code>thread::spawn()</code> block or apply to its handle inside the for loop(whilst disabling the external <code>.join()</code> ). But that makes all the threads execute in a chain which is not desirable, since the main intention of using threads was to execute same functions with different data values simultaneously.</p>
  <p>I am quite new to Rust in general(been 3 weeks since I am using it) and its my first time ever implementing Multithreading. I have only ever written single threaded programs in java and python before this, so if possible be a little noob friendly. However any help is appreciated :) .</p>