-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Expliciting lifetime in the first example #351
Open
Arthur-Milchior
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
Arthur-Milchior:lifetime_explicit_one
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,10 @@ explicitly is *extremely noisy*. All Rust code relies on aggressive inference | |
and elision of "obvious" things. | ||
|
||
One particularly interesting piece of sugar is that each `let` statement | ||
implicitly introduces a scope. For the most part, this doesn't really matter. | ||
However it does matter for variables that refer to each other. As a simple | ||
example, let's completely desugar this simple piece of Rust code: | ||
implicitly introduces a scope immediately after the declaration. For the most part, | ||
this doesn't really matter. However it does matter for variables that refer to | ||
each other. As a simple example, let's completely desugar this simple piece of | ||
Rust code: | ||
|
||
```rust | ||
let x = 0; | ||
|
@@ -47,21 +48,24 @@ likely desugar to the following: | |
|
||
<!-- ignore: desugared code --> | ||
```rust,ignore | ||
// 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; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Wow. That's... awful. Let's all take a moment to thank Rust for making this easier. | ||
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. | ||
Comment on lines
+65
to
+68
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 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. |
||
|
||
Actually passing references to outer scopes will cause Rust to infer | ||
a larger lifetime: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.