title: How to store rusqlite Connection and Statement objects in the same struct in Rust?
body: <p>I'm working on my first Rust program and have run afoul of Rust ownership semantics.  I have declared a <code>struct</code> which will encapsulate a SQLite database connection so it maintains a <code>Connection</code> member.  For performance reasons, I also want to keep a prepared statement, represented by the <code>Statement</code> type.  Here is a simplified version of my code:</p>
  
  <pre><code>extern crate rusqlite; // 0.14.0
  
  use rusqlite::{Connection, Statement};
  
  pub struct Foo&lt;'a&gt; {
      conn: Connection,
      statement: Statement&lt;'a&gt;,
  }
  
  impl&lt;'a&gt; Foo&lt;'a&gt; {
      pub fn new() -&gt; Foo&lt;'a&gt; {
          let conn = Connection::open(&amp;":memory:").unwrap();
          let statement = conn
              .prepare("INSERT INTO Foo(name, hash) VALUES($1, $2)")
              .unwrap();
          Foo { conn, statement }
      }
  }
  </code></pre>
  
  <p>I'm trying to transfer ownership of the <code>conn</code> variable to the callee by storing it in a member of <code>Foo</code>, but when I attempt to compile this code it fails:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `conn` does not live long enough
    --&gt; src/main.rs:13:25
     |
  13 |         let statement = conn
     |                         ^^^^ borrowed value does not live long enough
  ...
  17 |     }
     |     - borrowed value only lives until here
     |
  note: borrowed value must be valid for the lifetime 'a as defined on the impl at 10:6...
    --&gt; src/main.rs:10:6
     |
  10 | impl&lt;'a&gt; Foo&lt;'a&gt; {
     |      ^^
  </code></pre>
  
  <p>For some reason, the <code>rusqlite::Connection</code> type doesn't take a lifetime parameter, so I'm unable to explicitly tie its lifetime to that of the <code>Statement</code> instance.</p>
  
  <p>What am I missing?  This kind of encapsulation is a very common pattern, I'm sure I'm missing something.</p>