Closed
Description
#[derive(Copy, Clone)]
struct Bar;
fn foo(bar: Bar) -> impl FnOnce() -> Bar {
|| bar
}
(playground) main error message is:
error[E0373]: closure may outlive the current function, but it borrows `bar`, which is owned by the current function
--> src/lib.rs:5:5
|
5 | || bar
| ^^ --- `bar` is borrowed here
| |
| may outlive borrowed value `bar`
|
note: closure is returned here
I spent a long time trying to figure out where I was creating a reference until I finally figured out that Copy
types are always captured by reference, even if you move them in the closure. It would be helpful if it mentioned the fact that while this looks like a move it's actually copying out of a reference because the type is Copy
.