struct A {
f: Box<dyn Fn(i32) -> bool>
}
fn get(x: i32) -> bool {
x > 10
}
let a = A { f: Box::new(get) };
impl A {
fn to_get(&self) -> Box<dyn Fn(i32) -> bool> {
Box::new(
move |x: i32| (self.f)(x)
)
}
}
Error: lifetime may not live long enough
╭─[command_28:1:1]
│
2 │ fn to_get(&self) -> Box<dyn Fn(i32) -> bool> {
· ┬ │
· ╰────────────────────────────────── let's call the lifetime of this reference `'1`
· │
· ╰─ help: to declare that the trait object captures data from argument `self`, you can add an explicit `'_` lifetime bound: ` + '_`
3 │ ╭─▶ Box::new(
⋮ ⋮
5 │ ├─▶ )
· │
· ╰─────────────── returning this value requires that `'1` must outlive `'static`
The error says that the instance a
must outlive static, which means a
must static too?
So, when I need the method to_get
to return Box<dyn Fn(i32) -> bool>
, what is the right way to do it?