我正在尝试为Runge-Kutta解算器编写代码,其想法是应在“大”对象上解算ODE,因此应该是通用的,而不是在每一行都使用复制。我遇到了一个终身问题,将其提交给以下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;
}
我收到以下生命周期错误,我似乎无法解决。你能帮我打破现状吗?
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