Dear fellow rustians, this is my first post here and I'm sorry that this is basically just a new rant about lifetime annotations. But after more than a year of learning Rust, lifetime annotations simply don't stop to bother me. I simply don't get it, regardless how hard I try.
For me, lifetime annotations are the worst part of the language. If I had just one wish for the 2021 edition, that would be to get rid of them. have better Lifetime Elision.
Again I've spent the past few days learning all I can about lifetimes to fight my fear of them. I understand the syntax and I understand how to solve a lifetime problem in isolation. But when the code gets more complex, I am simply afraid of anything that has to do with them. There are a lot of other people who have asked similar questions, but non of the answers seems satisfying to me. People start talking about variance and covariance etc. but rarely say why lifetime annotations are actually useful. In every discussion I can clearly see how confusing lifetimes can be and that they are often very hard to explain to newcomers. But anyway, for some undefined reason, people want them to stay. Sometimes I think that I need to become a type theory expert to fully understand their usefulness. IMHO the book does not explain lifetimes well enough and don't even mentions anonymous lifetimes. For me, anonymous lifetimes are build around some intuition that some of you may have, but I don't. And there is no clear definition when it is possible to use them. Static lifetime can also be used in a lot more situations, that are not mentioned in the book. Writing lifetimes often feels like doing the job for my computer and all I want is that the compiler warns me, when a value doesn't live long enough. First time I heard how Rust manages memory, I thought, wow this is great and I simply implied that Rust does infer these things for me. But Rust doesn't. And I saw that it was the case in the earlier days? Why was it changed? Lifetimes do wake the fear in me of using references at all and it leads to a less performant code in my opinion. Lifetime-heavy code becomes very noisy and unreadable. Lifetimes also have a lot of subtle differences depending on where you put them and that makes the code hard to follow. When I'm writing a function that needs some lifetime annotations, I often feel like I need to know where it will be used, but why should I? Maybe I want that my function is reusable as much as possible and can't imply which lifetime is longer or shorter. Why do we need to type the lifetimes out when the compiler could analyze all possible variants and warn me when it finds a violation? This is what the computer is for! Or is this exactly the problem that would make compile times much longer? And there are a lot of misconceptions around lifetimes. This article (rust-blog/common-rust-lifetime-misconceptions.md at master · pretzelhammer/rust-blog · GitHub) led me actually realize how confusing and counterintuitive lifetimes are and that I'm not the only one who taps in all of those misconceptions.
In my eyes there are so many disadvantages to lifetime annotations, that there must be one or more >>huge<< advantages that justifies their existence. So please help me to get this once and for all. What is so helpful about lifetime annotations?
I've changed the title form "Why do you like lifetime annotations?" to "When do you find lifetime annotations helpful?", because it caused some confusion. I was really interested specifically in situation when do
lifetime annotations are most beneficial and not about the likability of this concept itself.
I'm sorry about this rant. Lifetime annotation can be tricky but now I do understand that they fulfill an important role.