Hello
I am a beginner to Rust. I simplified a bit this example and I am getting a different error:
Could you please point me to the cause/learning resources ?
error[E0597]: `group` does not live long enough
--> src/lifetime.rs:30:10
|
30 | test(&group);
| ^^^^^^ borrowed value does not live long enough
31 | }
| -
| |
| `group` dropped here while still borrowed
| borrow might be used here, when `group` is dropped and runs the destructor for type `lifetime::SimpleElementGroup<'_>`
SimpleElementGroup contains a Vec which implements Drop. When group goes out of scope Drop::drop is called before the stack is popped. But test asks to borrow group for it's whole lifetime, conflicting with Drop::drop.
You can learn more about Drop in this chapter of the book.
Thank you for the clarification.Now I understand why using a different life annotation works.
In this even simple example I am forcing a container to have the same lifetime as it's elements which is wrong (they are not "contained" by reference)