title: How to create threads in a for loop and get the return value from each?
body: <p>I am writing a program that pings a set of targets 100 times, and stores each RTT value returned from the ping into a vector, thus giving me a set of RTT values for each target. Say I have n targets, I would like all of the pinging to be done concurrently. The rust code looks like this:</p>
  <pre><code>    let mut sample_rtts_map = HashMap::new();
  
      for addr in targets.to_vec() {
          let mut sampleRTTvalues: Vec&lt;f32&gt; = vec![];
          //sample_rtts_map.insert(addr, sampleRTTvalues);
  
          thread::spawn(move || {
              while sampleRTTvalues.len() &lt; 100 {
                  let sampleRTT = ping(addr);
                  sampleRTTvalues.push(sampleRTT);
                  // thread::sleep(Duration::from_millis(5000));
              }
          });
      }
  </code></pre>
  <p>The hashmap is used to tell which vector of values belongs to which target. The problem is, how do I retrieve the updated sampleRTTvalues from each thread after the thread is done executing? I would like something like:</p>
  <pre><code>let (name, sampleRTTvalues) = thread::spawn(...)
  </code></pre>
  <p>The name, being the name of the thread, and sampleRTTvalues being the vector. However, since I'm creating threads in a for loop, each thread is being instantiated the same way, so how I differentiate them?</p>
  <p>Is there some better way to do this? I've looked into schedulers, future, etc., but it seems my case can just be done with simple threads.</p>