-
Notifications
You must be signed in to change notification settings - Fork 269
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
Expliciting lifetime in the first example #351
base: master
Are you sure you want to change the base?
Expliciting lifetime in the first example #351
Conversation
The main issue with having three lets one after the other, is that it's not clear if the lifetime created due to `let x` is the one before or after this line. Assuming I understood correctly what's going on, this example will clarify that the lifetime is created after the let. I also add a note stating what the usual notation is and warning that this longer explicit lifetime name is used only once for the sake of being as clear as possible. Other solutions I considered would be to add a "print" between two lets, this will ensure that the lifetime introduction is near a single "let", hence making it clear which "let" is concerned by the lifetime. If I got it wrong and actually the scope is introduced before the let statement, I guess at least this confirms that clarification may be helpful.
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.
I don't think changing lifetime names on later examples is easy to read, saying like "'a
implies the global lifetime" should be enough.
Note that the nomicon is less of a book for newcomers, that should defer to "the book" or something else.
I've read the book, and I must admit that, as far as I understand, it did not go in such gritty details as to indicate whether the lifetime technically starts before or after a variable declaration. Not that this distinction seemed useful in normal use of rust as far as I understand. Would it be okay to not only state that 'a is global, but
|
Co-authored-by: Yuki Okushi <[email protected]>
I beg your pardon, does "waiting-on-review" mean you wait for another reviewer, or that you wait for me to do a change? |
Wow. That's... awful. Let's all take a moment to thank Rust for making this | ||
easier. To make matter worse, traditionally, lifetimes are not as descriptive | ||
and simply called `'a`, `'b`, and so on. We will respect this tradition going | ||
forward. |
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.
I don't think this change is much helpful for readers and worth rephrasing so. As stated in the intro, we assume readers have a basic knowledge of Rust, actually, https://doc.rust-lang.org/nightly/book/ch10-03-lifetime-syntax.html#lifetime-annotation-syntax explains it and that should be enough.
// NOTE: `'a: {` and `&'b x` is not valid syntax! | ||
'a: { | ||
// NOTE: `'global_lifetime: {` and `&'x_lifetime x` is not valid syntax! | ||
'global_lifetime: { | ||
let x: i32 = 0; | ||
'b: { | ||
// lifetime used is 'b because that's good enough. | ||
let y: &'b i32 = &'b x; | ||
'c: { | ||
// ditto on 'c | ||
let z: &'c &'b i32 = &'c y; | ||
'x_lifetime: { | ||
// lifetime used is 'x_lifetime because that's good enough. | ||
let y: &'x_lifetime i32 = &'x_lifetime x; | ||
'y_lifetime: { | ||
// ditto on 'y_lifetime | ||
let z: &'y_lifetime &'x_lifetime i32 = &'y_lifetime y; |
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.
After some re-thinking of this, I still think this is too descriptive. We easily can find 'a
is a lifetime and curly braces are scopes if we're familiar with Rust's syntax. That is, naming lifetimes looks redundant, and tweaking prior wording should be enough here.
It implies a PR is waiting on review. I was going to do a second review. |
The main issue with having three lets one after the other, is that it's not
clear if the lifetime created due to
let x
is the one before or after thisline. Assuming I understood correctly what's going on, this example will clarify
that the lifetime is created after the let. I also add a note stating what the
usual notation is and warning that this longer explicit lifetime name is used
only once for the sake of being as clear as possible.
Other solutions I considered would be to add a "print" between two lets, this
will ensure that the lifetime introduction is near a single "let", hence making
it clear which "let" is concerned by the lifetime.
If I got it wrong and actually the scope is introduced before the let statement,
I guess at least this confirms that clarification may be helpful.
If the sake of clarity is more important than the sake of tradition, I'm willing to spend the time needed to add more explicit lifetime name to the other examples