struct TestStruct<F> {
param: F,
}
trait TestTrait {}
impl<F> TestTrait for TestStruct<F> {}
fn t<F>(input: F) -> Box<dyn TestTrait> {
Box::new(TestStruct { param: input }) // error here
}
error[E0310]: the parameter type `F` may not live long enough
--> src/main.rs:22:5
|
22 | Box::new(TestStruct { param: input }) //
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| the parameter type `F` must be valid for the static lifetime...
| ...so that the type `F` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound
|
21 | fn t<F: 'static>(input: F) -> Box<dyn TestTrait> {
| +++++++++
For more information about this error, try `rustc --explain E0310`.
This seems to happens only for generics, i tried input: Box<dyn trait>
/input: Struct
and some other types, maybe i’m wrong about only for generics
.
Add 'static
to fn t<F: 'static>
works.
And '_
works inside traits:
struct TestStruct<F> {
param: F,
}
trait TestTrait {}
impl<F> TestTrait for TestStruct<F> {}
impl<F> TestStruct<F> {
fn t(&self, input: F) -> Box<dyn TestTrait + '_> { // add `'_` here will compile, but not for the example above
// is it the same as adding `'static` at `impl<F: 'static>`?
Box::new(TestStruct { param: input })
}
}
I don’t understand why it’s needed and why 'static
.