title: How do I return a &Path from a function?
body: <p>I'm trying to understand how to write proper Rust code, but I think I may be overestimating the power of the compiler's ability to understand the lifetimes of my objects. This is the code as I expected it to work:</p>
  
  <pre><code>use std::path::Path;
  use std::env;
  use rusqlite::SqliteConnection;
  
  struct SomeDatabase {
      conn: SqliteConnection,
  }
  
  impl SomeDatabase {
      fn getPath() -&gt; &amp;Path {
          let path = env::home_dir().unwrap();
          path.push("foo.sqlite3");
          path.as_path()
      }
  
      fn open() -&gt; SomeDatabase {
          let path = SomeDatabase::getPath()
          SomeDatabase { conn: SqliteConnection::open(path).unwrap() }
      }
  }
  
  fn main() {
      let db = SomeDatabase::open();
  }
  </code></pre>
  
  <p>When I try to compile this, I get an error about a missing lifetime specifier on <code>&amp;Path</code>. I know if this took a reference parameter from the caller, it would take on the same lifetime as that reference has. Here though what I was expecting is that the lifetime would be attached to the variable I am assigning the result to.</p>
  
  <p>I know lifetimes can be added explicitly, but I don't know how to apply them in this case. The compiler suggests trying the <code>'static</code> lifetime, but that doesn't make sense here as far as I know because the source of this function's return value isn't static.</p>
  
  <p>Now, just to try to see what happened if I tried to compile the rest of the code, I changed the return type from <code>&amp;Path</code> to <code>PathBuf</code> and called <code>as_path()</code> in <code>open()</code>. This caused the compiler to output these errors:</p>
  
  <pre><code>src\main.rs:22:30: 22:52 error: the trait `core::marker::Sized` is not implemented for the type `[u8]` [E0277]
  src\main.rs:22         SomeDatabase { conn: SqliteConnection::open(path).unwrap() }
                                              ^~~~~~~~~~~~~~~~~~~~~~
  src\main.rs:22:30: 22:52 note: `[u8]` does not have a constant size known at compile-time
  src\main.rs:22         SomeDatabase { conn: SqliteConnection::open(path).unwrap() }
                                              ^~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  
  <p><code>SqliteConnection::open()</code> returns a <code>Result&lt;SqliteConnection, SqliteError&gt;</code> and the only field inside <code>SqliteConnection</code> is a <code>RefCell</code>, so I don't understand where this error about a byte array is coming from.</p>
  
  <p>So, why aren't things working as I expect and what is the most Rusty way to write this code? </p>