title: How to create a function returning a borrowed value?
body: <p>I have the following function as part of a Rust WASM application to convert a <code>Box</code>ed closure into the Rust-representation for a JavaScript function.</p>
  
  <pre><code>use js_sys::Function;
  
  type Callback = Rc&lt;RefCell&lt;Option&lt;Closure&lt;FnMut()&gt;&gt;&gt;&gt;;
  
  fn to_function(callback: &amp;Callback) -&gt; &amp;Function {
      callback.borrow().as_ref().unwrap().as_ref().unchecked_ref()
  }
  </code></pre>
  
  <p>However, the compiler complains that the return value uses a borrowed value (obtained with <code>callback.borrow()</code>) so cannot be returned.</p>
  
  <p>Hence, I decided to add lifetime annotations to inform the compiler that this new reference should live as long as the input.</p>
  
  <pre><code>use js_sys::Function;
  
  type Callback = Rc&lt;RefCell&lt;Option&lt;Closure&lt;FnMut()&gt;&gt;&gt;&gt;;
  
  fn to_function&lt;'a&gt;(callback: &amp;'a Callback) -&gt; &amp;'a Function {
      callback.borrow().as_ref().unwrap().as_ref().unchecked_ref()
  }
  </code></pre>
  
  <p>Unfortunately, this hasn't helped and I get the same error. What am I doing wrong here?</p>