rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.75k stars 258 forks source link

Expliciting lifetime in the first example #351

Open Arthur-Milchior opened 2 years ago

Arthur-Milchior commented 2 years ago

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.

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

Arthur-Milchior commented 2 years ago

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. If this was not in the book, I thought having one very detailed example could help.

Would it be okay to not only state that 'a is global, but

'a implies the global lifetime, 'b is the lifetime of x, and so on. That is, each lifetime begins immediately after the declaration.

Arthur-Milchior commented 2 years ago

I beg your pardon, does "waiting-on-review" mean you wait for another reviewer, or that you wait for me to do a change? I was waiting for an answer to my last question before potentially updating this PR

JohnTitor commented 2 years ago

I beg your pardon, does "waiting-on-review" mean you wait for another reviewer, or that you wait for me to do a change? I was waiting for an answer to my last question before potentially updating this PR

It implies a PR is waiting on review. I was going to do a second review.