2

I am trying to write code for a Runge-Kutta solver, with the idea that it should solve ODE on "big" object and hence should be generic and not use copy at every line. I have a lifetime issue which I have brought done to the following MCVE (playground):

use std::ops;

fn step<'a, 'b, Y, F>(f: F, y: &'a Y, t: f64) -> ()
where
    'a: 'b,
    F: Fn(&Y, f64) -> Y,
    f64: ops::Mul<&'b Y, Output = Y>,
{
    let k0 = f(y, t);
    let foo = 1. / 5. * &k0;
}

I get the following lifetime error, which I just can't seem to solve. Could you help me break done what is happening ?

error[E0597]: `k0` does not live long enough
  --> src/lib.rs:10:25
   |
3  | fn step<'a, 'b, Y, F>(f: F, y: &'a Y, t: f64) -> ()
   |             -- lifetime `'b` defined here
...
10 |     let foo = 1. / 5. * &k0;
   |                         ^^^
   |                         |
   |                         borrowed value does not live long enough
   |                         requires that `k0` is borrowed for `'b`
11 | }
   | - `k0` dropped here while still borrowed
NougatRillettes
  • 423
  • 2
  • 7
  • 2
    [One way to use an HRTB to solve your problem](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ca24577f858ac6e9511cc259f0c3b6e1), as the duplicate suggests. See [How do I write the lifetimes for references in a type constraint when one of them is a local reference?](https://stackoverflow.com/q/44343166/3650362) and also [How does for<> syntax differ from a regular lifetime bound?](https://stackoverflow.com/q/35592750/3650362) – trent May 09 '19 at 17:32

0 Answers0