title: How can I spawn asynchronous methods in a loop?
body: <p>I have a vector of objects that have a <code>resolve()</code> method that uses <code>reqwest</code> to query an external web API. After I call the <code>resolve()</code> method on each object, I want to print the result of every request.</p>
  <p>Here's my half-asynchronous code that compiles and works (but not really asynchronously):</p>
  <pre class="lang-rust prettyprint-override"><code>for mut item in items {
      item.resolve().await;
  
      item.print_result();
  }
  </code></pre>
  <p>I've tried to use <code>tokio::join!</code> to spawn all async calls and wait for them to finish, but I'm probably doing something wrong:</p>
  <pre class="lang-rust prettyprint-override"><code>tokio::join!(items.iter_mut().for_each(|item| item.resolve()));
  </code></pre>
  <p>Here's the error I'm getting:</p>
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
    --&gt; src\main.rs:25:51
     |
  25 |     tokio::join!(items.iter_mut().for_each(|item| item.resolve()));
     |                                                   ^^^^^^^^^^^^^^ expected `()`, found opaque type
     | 
    ::: src\redirect_definition.rs:32:37
     |
  32 |     pub async fn resolve(&amp;mut self) {
     |                                     - the `Output` of this `async fn`'s found opaque type
     |
     = note: expected unit type `()`
              found opaque type `impl std::future::Future`
  </code></pre>
  <p>How can I call the <code>resolve()</code> methods for all instances at once?</p>
  <hr />
  <p>This code reflects the answer - now I'm dealing with borrow checker errors that I don't really understand - should I annotate some of my variables with <code>'static</code>?</p>
  <pre class="lang-rust prettyprint-override"><code>let mut items = get_from_csv(path);
  
  let tasks: Vec&lt;_&gt; = items
      .iter_mut()
      .map(|item| tokio::spawn(item.resolve()))
      .collect();
  
  for task in tasks {
      task.await;
  }
  
  for item in items {
      item.print_result();
  }
  </code></pre>
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `items` does not live long enough
    --&gt; src\main.rs:18:25
     |
  18 |       let tasks: Vec&lt;_&gt; = items
     |                           -^^^^
     |                           |
     |  _________________________borrowed value does not live long enough
     | |
  19 | |         .iter_mut()
     | |___________________- argument requires that `items` is borrowed for `'static`
  ...
  31 |   }
     |   - `items` dropped here while still borrowed
  
  error[E0505]: cannot move out of `items` because it is borrowed
    --&gt; src\main.rs:27:17
     |
  18 |       let tasks: Vec&lt;_&gt; = items
     |                           -----
     |                           |
     |  _________________________borrow of `items` occurs here
     | |
  19 | |         .iter_mut()
     | |___________________- argument requires that `items` is borrowed for `'static`
  ...
  27 |       for item in items {
     |                   ^^^^^ move out of `items` occurs here
  </code></pre>