So somebody once said that you should not hesitate to post things you think would be n00bish but others might simply struggle with the same problems so it is helpful anyway. While I couldn’t agree more it is still a step to really do it.
Writing it down will hopefully also help me getting better at it.
I am currently focussing more and more on rust (this time for real!). In my current project I somehow managed to avoid them altogether so far but suddenly I saw it again:
So once again I read through the lifetime chapter. I always think I understand the concept and think it is super awesome but I just fail at implementation. And here again. I failed. I have a main struct which should own most of the things. One of the members would be an optional member that should borrow one of the other members for a time. I know that the main struct will outlive every member.
So a stupid example (without lifetimes for now):
Obviously an artificial example because why should
Wurst be optional in a
Meal - but whatever.
Wurst should be created and be able to access the
As we are borrowing something we need a lifetime specifier for
Wurst like so:
Now comes my understanding of it (BEWARE - might be totally wrong)
Line by line:
This just declares a lifetime specifier. It does not do anything special except for the declaration.
'a and says:
sauce has the lifetime of
'a. We could use
'a for more members here to indicate that
they share a common lifetime.
Now to using it:
This also declares a lifetime
'a for the
Meal and uses it for
Wurst. So far so good.
'a can also be
'b or whatever.
The name need not be the
'a we used
Wurst. Even when using a different specifier they still mean the same thing, namely that
Wurst has some lifetime but the name of the lifetime specifier is arbitrary.
Much like we can fiddle with variable names:
Now comes the part where I was stuck for a while. Let’s go through here step by step.
First the compiler will remind us that the
impl Meal needs some lifetime (because the struct has been declared with one).
That is easy to fix:
Again we are declaring a lifetime specifier (immediately after impl) and then use it (right after
Having fixed that the compiler is completly going wild:
So that left me head-scratching for a while. Everything was (to my knowledge) well defined on the struct and it was still complaining. Somehow rust still couldn’t figure out that
Meal would live longer. I tried everything to set some lifetime when creating the
Wurst but nothing helped. In fact I was messing at the wrong place.
To actually let rust know about the lifetime of a reference you need to specify the lifetime where it is actually created and not where it is used. This might seem trivial (even to my later self) but I just couldn’t get my mind around it at that time :)
So the solution was:
And here is the full code:
Happy Wursting and Rusting.