title: Function refered from the stored in struct does not give the ownership up
body: <p>I'm writing a algorithm testbench to compare performance in Rust.</p>
  
  <p>I want to store a bunch of function for an algorithm in a struct, and apply those functions to some data.
  When I call the function by reference, which stored in the struct, I couldn't figure out the lifetime.</p>
  
  <pre class="lang-rust prettyprint-override"><code>struct Alg&lt;'a, 'b, 'c&gt; {
      alg1: &amp;'c Fn(&amp;'a A&lt;'a&gt;, &amp;'b B&lt;'b&gt;) -&gt; usize,
      alg2: &amp;'c Fn(&amp;'a A&lt;'a&gt;, &amp;'b B&lt;'b&gt;) -&gt; String,
  }
  
  struct A&lt;'a&gt; {
      a_str: &amp;'a str,
  }
  
  struct B&lt;'b&gt; {
      b_str: &amp;'b str,
  }
  
  fn concat&lt;'a, 'b&gt;(_a: &amp;'a A&lt;'a&gt;, _b: &amp;'b B&lt;'b&gt;) -&gt; String {
      _a.a_str.to_string() + &amp;_b.b_str.to_string()
  }
  
  fn length&lt;'a, 'b&gt;(_a: &amp;'a A&lt;'a&gt;, _b: &amp;'b B&lt;'b&gt;) -&gt; usize {
      _a.a_str.len() + _b.b_str.len()
  }
  
  fn run1&lt;'a, 'b, 'c&gt;(_a: &amp;'a A&lt;'a&gt;, _b: &amp;'b B&lt;'b&gt;, _f_c: &amp;'c Alg&lt;'a, 'b, 'c&gt;) {
      println!("{}", &amp;(_f_c.alg1)(_a, _b));
  }
  
  fn run2&lt;'a, 'b, 'c&gt;(_a: &amp;'a A&lt;'a&gt;, _b: &amp;'b B&lt;'b&gt;, _f_c: &amp;'c Alg&lt;'a, 'b, 'c&gt;) {
      println!("{}", &amp;(_f_c.alg2)(_a, _b));
  }
  
  fn main() {
      let f_struct = Alg {
          alg1: &amp;length,
          alg2: &amp;concat,
      };
  
      for _i in 0..2 {
          let a_str = "ABC";
          let a = A { a_str: a_str };
          for _j in 0..2 {
              let b_str = "BCD";
              let b = B { b_str: b_str };
              println!("{}", concat(&amp;a, &amp;b)); // This works
              println!("{}", (f_struct.alg1)(&amp;a, &amp;b)); // I expect that `concat` or `length` in `f_struct` may finished borrowing `a` or `b' here, as like as `println!("{}",concat(&amp;a,&amp;b))`
                                                       //run1(&amp;a,&amp;b,&amp;f_struct);
                                                       //run2(&amp;a,&amp;b,&amp;f_struct);
          }
      }
  }
  </code></pre>
  
  <p>When I run this, I get an error message like:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `a` does not live long enough
    --&gt; src/main.rs:43:44
     |
  43 |             println!("{}", (f_struct.alg1)(&amp;a, &amp;b)); // I expect that `concat` or `length` in `f_struct` may finished borrowing `a` or `b' here, as like as `println!("{}",concat(&amp;a,&amp;b))`
     |                            --------------- ^^ borrowed value does not live long enough
     |                            |
     |                            borrow used here, in later iteration of loop
  ...
  47 |     }
     |     - `a` dropped here while still borrowed
  
  error[E0597]: `b` does not live long enough
    --&gt; src/main.rs:43:48
     |
  43 |             println!("{}", (f_struct.alg1)(&amp;a, &amp;b)); // I expect that `concat` or `length` in `f_struct` may finished borrowing `a` or `b' here, as like as `println!("{}",concat(&amp;a,&amp;b))`
     |                            ---------------     ^^ borrowed value does not live long enough
     |                            |
     |                            borrow used here, in later iteration of loop
  ...
  46 |         }
     |         - `b` dropped here while still borrowed
  </code></pre>
  
  <p>What is the difference between <code>println!("{}",concat(&amp;a,&amp;b))</code> and <code>println!("{}",(f_struct.alg1)(&amp;a,&amp;b))</code>?</p>
  
  <p>I thought that I have to indicate something that the function no more borrows the value with lifetime <code>'a</code> or <code>'b</code>, but I couldn't find it from rust-by-example or rust-book.</p>
  
  <p>I've tried to apply coercion like <code>'c: 'a + 'b</code>, but this seems not to help.</p>
  
  <p>These questions are related, but not so clear to me.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/41204134/rust-lifetime-error">Rust lifetime error</a></li>
  <li><a href="https://stackoverflow.com/questions/24847331/rust-lifetime-error-expected-concrete-lifetime-but-found-bound-lifetime">Rust lifetime error expected concrete lifetime but found bound lifetime</a></li>
  <li><a href="https://stackoverflow.com/questions/43223497/questions-about-rust-lifetime">Questions about Rust lifetime</a></li>
  </ul>
  
  <h3>Point</h3>
  
  <ul>
  <li>I want to store functions in the struct
  
  <ul>
  <li>I could try another way like not to store functions in the struct</li>
  <li>But I want to understand the reason when this approach is impossible</li>
  </ul></li>
  </ul>