I'm a bit confused about how capturing works for references in the environment. Take the following example:
fn make_add(l: &i32) -> impl Fn(i32) -> i32 + '_ {
|r| l + r
}
This doesn't compile because l
is borrowed in the closure which outlives the function:
error[E0373]: closure may outlive the current function, but it borrows
l
, which is owned by the current function
The error message further suggests to use move
to make the closure take ownership and this works:
move |r| l + r
But I struggle to understand why exactly move
is needed here. It doesn't actually move the object l
references into the closure (this construct also works for types that are not Copy
) so it seems as if without move, l
would be &&i32
inside the closure which would explain the error. However, when asking the compiler to print the type of l
(e.g., by writing l.x
), it insists it is &i32
.
What am I missing here?