-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Book: Be very explicit of lifetimes being descriptive #36997
Changes from 4 commits
2130637
7f02eb3
a542302
2a0bd6d
0d0cdb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,8 @@ To fix this, we have to make sure that step four never happens after step | |
three. The ownership system in Rust does this through a concept called | ||
lifetimes, which describe the scope that a reference is valid for. | ||
|
||
**Note** It's important to understand that lifetime annotations are _descriptive_, not _prescriptive_. This means that how long a reference is valid is determined by the code, not by the annotations. The annotations, however, point out this fact to the compiler in case it can't figure it out by itself. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So, the compiler doesn't try to "figure out" the right lifetimes; it only applies the elision rules, which are just dumb patterns. So I would change this to something like,
Or something like that. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't really understand the inner workings of the compiler, but my thinking goes like this: Lifetime rules are for figuring out if it's ok do drop a certain value at the point where it goes out of scope (i.e. it's owner). For that, I assume the compiler does a "control lifetimes" pass, and if that's ok, it forgets about them and translates the code as is, without resorting to any lifetime anymore (this is what this is about, you can't influence the machine code by lifetimes, ony if it compiles or not). Elision let's the programmer leave out certain annotation, but that "just" means the compiler puts them in itself. After elision, it still has the "real work" of checking. In that way, lifetime annotations "help the compiler figure out if the code is ok to compile". Is that correct? I'll try to improve my wording, but I'll change it if my thoughts aren't correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct. |
||
|
||
When we have a function that takes an argument by reference, we can be | ||
implicit or explicit about the lifetime of the reference: | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you wrap this to 80 columns please?