title: Error on Future generator closure: Captured variable cannot escape `FnMut` closure body
body: <p>I want to create a simple websocket server. I want to process the incoming messages and send a response, but I get an error:</p>
  <pre class="lang-none prettyprint-override"><code>error: captured variable cannot escape `FnMut` closure body
    --&gt; src\main.rs:32:27
     |
  32 |       incoming.for_each(|m| async {
     |  _________________________-_^
     | |                         |
     | |                         inferred to be a `FnMut` closure
  33 | |         match m {
  34 | |             // Error here...
  35 | |             Ok(message) =&gt; do_something(message, db, &amp;mut outgoing).await,
  36 | |             Err(e) =&gt; panic!(e)
  37 | |         }
  38 | |     }).await;
     | |_____^ returns a reference to a captured variable which escapes the closure body
     |
     = note: `FnMut` closures only have access to their captured variables while they are executing...
     = note: ...therefore, they cannot allow references to captured variables to escape
  </code></pre>
  <p>This gives a few hits on Stack Overflow but I don't see anywhere in my code where a variable is escaping. The async block won't run concurrently, so I don't see any problem. Furthermore, I feel like I am doing something very simple: I get a type which allows me to send data back to the client, but when using a reference to it in the async block, it gives a compile error. The error only occurs when I use the <code>outgoing</code> or <code>db</code> variable in the async code.</p>
  <p>This is my code (error is in the <code>handle_connection</code> function):</p>
  <p><strong>main.rs</strong></p>
  <pre><code>use tokio::net::{TcpListener, TcpStream};
  use std::net::SocketAddr;
  use std::sync::Arc;
  use futures::{StreamExt, SinkExt};
  use tungstenite::Message;
  use tokio_tungstenite::WebSocketStream;
  
  struct DatabaseConnection;
  
  #[tokio::main]
  async fn main() -&gt; Result&lt;(), ()&gt; {
      listen(&quot;127.0.0.1:3012&quot;, Arc::new(DatabaseConnection)).await
  }
  
  async fn listen(address: &amp;str, db: Arc&lt;DatabaseConnection&gt;) -&gt; Result&lt;(), ()&gt; {
      let try_socket = TcpListener::bind(address).await;
      let mut listener = try_socket.expect(&quot;Failed to bind on address&quot;);
  
      while let Ok((stream, addr)) = listener.accept().await {
          tokio::spawn(handle_connection(stream, addr, db.clone()));
      }
  
      Ok(())
  }
  
  async fn handle_connection(raw_stream: TcpStream, addr: SocketAddr, db: Arc&lt;DatabaseConnection&gt;) {
      let db = &amp;*db;
      let ws_stream = tokio_tungstenite::accept_async(raw_stream).await.unwrap();
  
      let (mut outgoing, incoming) = ws_stream.split();
  
      // Adding 'move' does also not work
      incoming.for_each(|m| async {
          match m {
              // Error here...
              Ok(message) =&gt; do_something(message, db, &amp;mut outgoing).await,
              Err(e) =&gt; panic!(e)
          }
      }).await;
  }
  
  async fn do_something(message: Message, db: &amp;DatabaseConnection, outgoing: &amp;mut futures_util::stream::SplitSink&lt;WebSocketStream&lt;TcpStream&gt;, Message&gt;) {
      // Do something...
  
      // Send some message
      let _ = outgoing.send(Message::Text(&quot;yay&quot;.to_string())).await;
  }
  </code></pre>
  <p><strong>Cargo.toml</strong></p>
  <pre><code>[dependencies]
  futures = &quot;0.3.*&quot;
  futures-channel = &quot;0.3.*&quot;
  futures-util = &quot;0.3.*&quot;
  tokio = { version = &quot;0.2.*&quot;, features = [ &quot;full&quot; ] }
  tokio-tungstenite = &quot;0.10.*&quot;
  tungstenite = &quot;0.10.*&quot;
  </code></pre>
  <p>When using <code>async move</code>, I get the following error:</p>
  <p><strong>code</strong></p>
  <pre><code>incoming.for_each(|m| async move {
      let x = &amp;mut outgoing;
      let b = db;
  }).await;
  </code></pre>
  <p><strong>error</strong></p>
  <pre class="lang-none prettyprint-override"><code>error[E0507]: cannot move out of `outgoing`, a captured variable in an `FnMut` closure
    --&gt; src\main.rs:33:38
     |
  31 |       let (mut outgoing, incoming) = ws_stream.split();
     |            ------------ captured outer variable
  32 | 
  33 |       incoming.for_each(|m| async move {
     |  ______________________________________^
  34 | |         let x = &amp;mut outgoing;
     | |                      --------
     | |                      |
     | |                      move occurs because `outgoing` has type `futures_util::stream::stream::split::SplitSink&lt;tokio_tungstenite::WebSocketStream&lt;tokio::net::tcp::stream::TcpStream&gt;, tungstenite::protocol::message::Message&gt;`, which does not implement the `Copy` trait
     | |                      move occurs due to use in generator
  35 | |         let b = db;
  36 | |     }).await;
     | |_____^ move out of `outgoing` occurs here
  </code></pre>