While talking about what precisely triggers that error that @irfan-hossain-bhuiya is seeing have merit practically it's better to do what @vague have done and explain why such change makes sense.
Because every time I see something like struct WrappedToken<'a>(&'a Token<'a>)
or fn to(&'a self)->WrappedToken<'a>;
I just know that what I would see after that point is some kind of nonsense lifetime markup code which wouldn't compile.
To understand why you don't need to understand all these precise nuances much, you just do a quick glance and see code which, most likely, wouldn't work.
Because lifetime markup marks:
- Don't affect the generated code.
- Are, nonetheless, incredibly important.
What they are doing if they don't affect the generated code? The add formal proof of correctness to your code. And compiler just simply verifies that said proof of correctness is actually valid. That's it.
Now, suppose you are solving some math problem and use a
to denote diameter of steering wheel in your car and also length of steering column and also width of the driver seat… I wouldn't expect such proof to be correct. In fact math teacher would just reject such “proof” after reading first few lines and criss-crossing everything by just adding note “steering wheel diameter and width of driver seat, may be different, please redo”.
Similarly here: talking about what kind of “insane lifetimes markup” may still work and how is an interesting intellectual exercise but for newbies the first rule they should learn is: never give identical names for lifetimes except if you have some reason to make them identical.
In that code that we had in the very first message WrappedToken
had the exact same lifetime as Token
that it wraps. And then to
merges two lifetimes, too. Why would you do that? To make it impossible to write sane code in main
? Congrats, you have achieved that.
Indeed, the very first message includes that passage:
It's like writing on the sticker “brick of width 'a
, length 'a
, width 'a
” and then try to sell real, non-cubical, brick under such banner and complaining that no one wants to buy it and everyone tells you that you are not giving them what you promised. You told the compiler that it's not Ok to die when the pointer dies thus you are stuck with that promise. Either change the sticker or brick, there are no way to declare one thing and then sell another in Rust.