-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Non lexical lifetimes not mentioned in Chapter 4 #2077
Comments
I agree that the book is not super clear on this point, but not all of the mentions of "scope" in the chapter are incorrect:
With or without NLL, borrow checking does not influence when values are dropped. These sentences are just describing how RAII works in Rust.
Same but in the negative. The value of
Is true, but I'm not sure why the book highlights this; I would not have assumed otherwise. |
I agree that the points you mentioned are correct. But combined with the other points I mentioned, they create the idea that lifetime of borrows is tied to scope. I have mentioned this here
The purpose of adding those lines was to point out how scope is emphasised while no mention is made of a borrow's lifetime. Of course, the first and fourth points under chapter 4-02 are technically not correct
So my suggestion is that all occurrences of "scope" after a brief subsection on lifetime of a borrow in chapter 4-02, be edited. |
Closing as a duplicate of #1939. Thanks! |
Brief Description
There is no mention of non-lexical lifetimes, or lifetimes at all for that matter, in chapter 4, leading to questions such as this and the comments below the question.
Details
The issue is that non-lexical lifetimes, and lifetimes themselves are not discussed in the context of ownership and references. In fact, chapter 4 is full of lines which imply that the lifetime of a borrow is tied to its lexical scope, and there are none that suggest otherwise. Thus, someone approaching rust from a different language or an earlier version of rust (2015 edition for instance) will have no idea that an unused mutable borrow might have a more limited lifetime than the block scope. This is what led me to ask that SO question. I list out the lines from the book which discuss the issue of when a reference is invalidated. While the lines from chapter 4 section 01 concern ownership and not references, sections 01 and 02 form a continuum when reading the book. When they are read together, it leaves the reader with the strong impression that the lifetime of a variable and by extension references to it are restricted by lexical scope. Here are the lines:
The code sample above, listed in the book, is one of the textbook use cases where NLL actually makes a difference if
r1
is not used any further.Summary
There is no mention of lifetimes and the possibility that a mutable borrow might have a lifetime smaller than its lexical scope. Someone coming from C++ or rust 2015 might be justified in assuming that a mutable borrow is its lexical scope and might be confused by the compiler's output as I was in the SO question.
Proposed Solution
I suggest that in the discussion about references and borrowing in chapter 4 section 02, immediately after references are introduced:
The text was updated successfully, but these errors were encountered: