I’m quite confused by a problem with lifetimes.
I have a function, find_root
, which finds a root by Newton’s Method. Its signature is:
type RealFn<N> where N: Float + Div + Sub + PartialOrd = Fn(N) > N;
pub fn find_root<'a, N>(function: &'a RealFn<N>, derivative: &'a RealFn<N>, x0: &N, acceptable_err: &N, max_iterations: i32) > Option<N>
where N: Float + Div + Sub + PartialOrd
I then have a method for finding square roots:
fn inv_sqrt<N>(radicand: &N, x: N) > N
where N: Float + PartialOrd + Sub + Mul + Div {
x.powi(2)  *radicand
}
fn inv_sqrt_derivative<N>(x: N) > N
where N: Float + PartialOrd + Sub + Mul + Div + From<f64> {
x * 2.0.into()
}
pub fn find_sqrt<'a, N>(radicand: &'a N, x0: &'a N,
acceptable_err: &'a N, max_iterations: i32)
> Option<N>
where N: Float + Div + Sub + Mul + PartialOrd + From<f64> {
let specific_inv_sqrt: &'a Fn(N) > N= &move x {
inv_sqrt(radicand, x)
};
find_root(&specific_inv_sqrt, &inv_sqrt_derivative, x0, acceptable_err, max_iterations)
}
This does not compile. I get this error:
Compiling nrfind v0.1.1 (file:///home/leo/Projects/nrfind)
error[E0477]: the type `&'a std::ops::Fn(N) > N + 'a` does not fulfill the required lifetime
> src/sqrt.rs:22:15

22  find_root(&specific_inv_sqrt, &inv_sqrt_derivative, x0, acceptable_err, max_iterations)
 ^^^^^^^^^^^^^^^^^^

= note: type must outlive the static lifetime
error: aborting due to previous error
error: Could not compile `nrfind`.
To learn more, run the command again with verbose.
Unfortunately, I’ve tried a lot of things (as you can see with my flailing type annotations) and I have looked at a number of resources, but I have absolutely no idea what is going on. Please help?