title: Rust: Closure composition ownership
body: <p>To avoid nesting matches I am trying to use the <code>and_then</code> method for <code>Result</code>. The problem arises when I try to convert this</p>
  <pre><code>match Connection::open(&amp;db_path) {
      Ok(conn) =&gt; {
          match conn.prepare(&quot;SELECT qwe,username_value,wer FROM my_table;&quot;) {
              Ok(mut stmt) =&gt; {
                  match stmt.query_map([], |row| {Ok(row.get_unwrap::&lt;usize, String&gt;(1).clone())}) {
                      Ok(usernames) =&gt; {
                          for username in usernames {
                              if username.is_ok() {
                                  println!(&quot;{}&quot;, username.expect(&quot;Could not read username value&quot;));
                              }
                          }
                          println!(&quot;Query done&quot;);
                      },
                      Err(err) =&gt; println!(&quot;Error {}&quot;, err),
                  }
              },
              Err(err) =&gt; println!(&quot;Error {}&quot;, err),
          }
      },
      Err(err) =&gt; println!(&quot;Error {}&quot;, err),
  }
  </code></pre>
  <p>into this</p>
  <pre><code>match Connection::open(&amp;db_path)
      .and_then(move |conn: Connection| conn.prepare(&quot;SELECT qwe,username_value,wer FROM my_table;&quot;))
      .and_then(|mut stmt: Statement| stmt.query_map([], |row| {Ok(row.get_unwrap::&lt;usize, String&gt;(1).clone())})) {
      Ok(usernames) =&gt; {
          for username in usernames {
              if username.is_ok() {
                  println!(&quot;{}&quot;, username.expect(&quot;Could not read username value&quot;));
              }
          }
          println!(&quot;Query done&quot;);
      },
      Err(_) =&gt; println!(&quot;Error querying db&quot;),
  }
  </code></pre>
  <p>The SQL lib is rusqlite.</p>
  <p>The first snippet is accepted by the compiler, the second one is not.
  The compiler screams at me with the following error message:</p>
  <pre><code>error[E0515]: cannot return value referencing function parameter `conn`
     --&gt; src\main.rs:134:43
      |
  134 |         .and_then(move |conn: Connection| conn.prepare(&quot;SELECT qwe,username_value,wer FROM my_table;&quot;))
      |                                           ----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |                                           |
      |                                           returns a value referencing data owned by the current function
      |                                           `conn` is borrowed here
  
  error[E0515]: cannot return value referencing function parameter `stmt`
     --&gt; src\main.rs:135:41
      |
  135 |         .and_then(|mut stmt: Statement| stmt.query_map([], |row| {Ok(row.get_unwrap::&lt;usize, String&gt;(1).clone())})) {
      |                                         ----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |                                         |
      |                                         returns a value referencing data owned by the current function
      |                                         `stmt` is borrowed here
  
  error: aborting due to 2 previous errors
  </code></pre>
  <p>I tried with <code>move</code> on the three closures but the error message is the same.
  Why is the ownership wrong in the second snippet and which is the idiomatic way of writing something like this?</p>