On Thu, Jun 12, 2025 at 11:00 AM Zhao Liu <[email protected]> wrote:
> > +/// It's impossible to escape the `Jail`; `token1` cannot be moved out of
> > the
> > +/// closure:
> > +///
> > +/// ```ignore
> > +/// let x = 42;
> > +/// let escape = Jail::with(&x, |token1| {
> > +/// println!("{}", token1.get());
> > +/// token1
>
> This line will fail to compile (the below comment "// fails to compile" seems
> to indicate that println! will fail):
>
> error: lifetime may not live long enough
> --> src/main.rs:22:9
> |
> 20 | let escape = Jail::with(x, |token1| {
> | ------- return type of closure is
> Jail<'2, i32>
> | |
> | has type `Jail<'1, i32>`
> 21 | println!("{}", token1.get());
> 22 | token1
> | ^^^^^^ returning this value requires that `'1` must outlive `'2`
Right, I put it there because '2 lives until the second println!. The
problem is not so much that it's returning token1, it's that the
println uses it.
I can see that it's confusing, maybe:
// Because "escape" is used after the closure has returned, the
// compiler cannot find a type for the "let escape" assignment.
Paolo