I encountered a compile error related to lifetime in my Rust code. Here is the code causing the error:
fn to_pointer_vec(ptrs: &[*const dyn ToString]) -> Vec<*const dyn ToString> {
let mut vec: Vec<*const dyn ToString> = Vec::new();
for &ptr in ptrs {
vec.push(ptr);
}
vec
}
The error message can be seen below:
--> src/table/merger.rs:78:9
|
73 | fn to_pointer_vec(ptrs: &[*const dyn ToString]) -> Vec<*const dyn ToString> {
| - let's call the lifetime of this reference `'1`
...
78 | vec
| ^^^ returning this value requires that `'1` must outlive `'static`
|
help: to declare that the trait object captures data from argument `ptrs`, you can add an explicit `'_` lifetime bound
|
73 | fn to_pointer_vec(ptrs: &[*const dyn ToString]) -> Vec<*const dyn ToString + '_> {
| ++++
I don't understand why the trait object pointer has a lifetime constraint.