Lifetimes arent my strong suit, and I dont really get what the error is saying, I have tried playing with the bounds, but I just cant seem to find a solution that compiles.
(this is part of a more complex system, but this shows the error) ```rust
struct NonCopy;
struct Hello(NonCopy);
struct Guard<F>(F);
impl<F> Guard<F> {
fn new<'a, C, R>(func: F) -> Self
where
F: Fn(&'a C) -> R,
C: 'a,
R: 'a,
{
Self(func)
}
}
impl Hello {
fn get<'a, F, R>(&'a self, guard: &Guard<F>) -> R
where
F: Fn(&'a Self) -> R,
R: 'a,
{
(guard.0)(self)
}
}
fn main() {
let x = Guard::new(|hello: &Hello| &hello.0);
let _z = move |hello: &Hello| {
let _res = hello.get(&x);
};
}
```rust
error[E0521]: borrowed data escapes outside of closure
--> src/main.rs:30:20
|
27 | let x = Guard::new(|hello: &Hello| &hello.0);
| - `x` declared here, outside of the closure body
28 |
29 | let _z = move |hello: &Hello| {
| ----- `hello` is a reference that is only valid in the closure body
30 | let _res = hello.get(&x);
| ^^^^^^^^^^^^^ `hello` escapes the closure body here
I am not really sure where its "escaping"? I dont return the result of the get call, and I thought the lifetime bounds were correct.
The only examples I get of this error when googling is stuff like pushing into a vec outside the closure, which I get, but I thought i was moveing the guard into the closure? and the closure isnt even storing the value.