The following code can't compile(playground):
struct StructFunc {
f: fn(usize, usize) -> bool,
}
impl StructFunc {
fn gen_closure<'a>(&self, data: &'a Vec<usize>) -> Box<dyn Fn(usize) -> bool + 'a> {
Box::new(
move |i: usize| (self.f)(i, data[i])
)
}
}
This is the error:
error: lifetime may not live long enough
--> src\plot.rs:51:9
|
50 | fn gen_closure<'a>(&self, data: &'a Vec<usize>) -> Box<dyn Fn(usize) -> bool + 'a> {
| -- - let's call the lifetime of this reference `'1`
| |
| lifetime `'a` defined here
51 | / Box::new(
52 | | move |i: usize| (self.f)(i, data[i])
53 | | )
| |_________^ associated function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`
I trying to let it work like:
fn greater(x: usize, y: usize) -> bool {
x > y
}
let struct_fun = StructFunc(greater);