Closed
Description
I tried this code:
trait FooTrait {}
struct Foo<'a> {
value: i32,
x: &'a mut dyn FooTrait,
}
impl<'a> Foo<'a> {
fn works(&self) -> Box<dyn FnOnce() -> i32 + '_> {
Box::new(|| self.value)
}
fn doent(&self) -> impl FnOnce() -> i32 + '_ {
|| self.value
}
}
I expected both methods to compile, but only the dyn version does.
The error says:
error[E0700]: hidden type for `impl FnOnce() -> i32 + '_` captures lifetime that does not appear in bounds
--> src/lib.rs:13:9
|
8 | impl<'a> Foo<'a> {
| -- hidden type `{closure@src/lib.rs:13:9: 13:11}` captures the lifetime `'a` as defined here
...
12 | fn doent(&self) -> impl FnOnce() -> i32 + '_ {
| ------------------------- opaque type defined here
13 | || self.value
| ^^^^^^^^^^^^^
The error says that the closure captures lifetime 'a
, although the closure in the dyn version is the same and compiles successfully without capturing 'a
Tried both current stable and nightly