Getting Started with Rust: Reference and Lifetime
mathieu-nivoliez.com
mathieu-nivoliez.com
While this can be true, you are always dealing with them if you have a reference to something. Sometimes the compiler will elide them for you but other times you need to explicitly write them. The code snippets in this blog need lifetime annotations for example.
I still not figure what the hell is the deal with it. Why rust no "just" give a "string for mortals"?
Once you understand this concept you see it over and over. It's how every type in the language works (unless that type implements the Copy trait).
I actually think this is something really fundamental and amazing the more I think about it. Most languages, like Java, tell you to make defensive copies of your data because you never know who will be modifying it later. Also most languages will enforce that all strings are always immutable, to prevent people from needing to make defensive copies. Rust just naturally handles this with it's ownership and borrowing rules. And then the very same concept is what makes concurrency safe.
I wish it have a "easy mode" and a "advanced" one for when is necessary.
As you continue to use Rust you will find that each type has their reason and string slices (what are used in this blog post) have their limits.
[0]: https://doc.rust-lang.org/std/string/struct.String.html
Surely the compiler isn't going to remove or ignore things that you typed.
You probably meant to say the compiler will allow you to elide them.
I have spent some time reading about rust in the past but rarely actually used it.
When reading about rust I am constantly bombarded with what I would describe as lazy terminology.
In this case, the same term, `lifetime`, is being used to describe several distinct aspects of a system. Between the link you shared, the "Rust Book" that is available online, the book "Programming Rust", the compiler's error messages, and all the various blog posts, there is a definite lack of clear language. Many of these sources are even inconsistent within themselves.
It seems like the clearest, most agreed usage is that "lifetime" in general is very close to a concept many other languages have, usually called "scope".
The problem starts when eventually all these sources start talking about these things that look like `'a`. Suddenly they're calling `'a` the `lifetime`. That's a poor reuse of the same term.
It appears the compiler's error messages are calling this a `lifetime parameter`. The "Rust Book" at one point calls them `lifetime annotations`. Many sources just continue calling them `lifetimes` over and over again.
It would be nice if someone could come up with very precise language to describe each aspect of the system so that anyone writing about it can communicate clearly.
This problem is not limited to lifetimes at all. As I mentioned above, almost every time I read about rust I'm finding this reuse of terms, misuse of terms, and lack of precision. It's quite frustrating and a major turn off.
http://smallcultfollowing.com/babysteps/blog/2016/04/27/non-...
I'm sure someone out there unfamiliar with types thinks it's inconsistent, but it's not really something you think about once you familiarize.
I was curious enough to grab a nearby textbook on Java. The author very clearly calls these things "type parameters" or "type variables". Imagine trying to write the whole chapter just calling them "types".....
"Your generic type will have two other types T and E and we will fill in Grape as type T and Fred as type E...."
It'd be a disaster! Instead he's used clear language, every single time he refers to one of those things, he uses the specific term. Much better experience.