I was trying to understand closures in rust and thus was trying to return a closure from the method of a struct but it seems to be failing. I was trying to define a line and getting a predictor from the line as below
struct line {
pub constant: f64,
pub slope: f64,
}impl line{
pub fn get_predictor(&self) -> impl Fn(f64) -> f64{
move |b| self.slope * b + self.constant
}
}
However I seem to see the following failure due to this
error[E0700]: hidden type for
impl Trait
captures lifetime that does not appear in bounds
--> line/src/main.rs:11:5
|
10 | pub fn get_predictor(&self) -> impl Fn(f64) -> f64{
| ----- hidden type[closure@line/src/main.rs:11:5: 11:44]
captures the anonymous lifetime defined here
11 | move |b| self.slope * b + self.constant
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: to declare that theimpl Trait
captures'_
, you can add an explicit'_
lifetime bound
|
10 | pub fn get_predictor(&self) -> impl Fn(f64) -> f64 + '_{
Any pointers to what I'm getting wrong here?