-
Notifications
You must be signed in to change notification settings - Fork 13.3k
docs: named lifetimes #13106
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
docs: named lifetimes #13106
Changes from all commits
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 |
---|---|---|
|
@@ -559,9 +559,14 @@ points at a static constant). | |
|
||
# Named lifetimes | ||
|
||
Let's look at named lifetimes in more detail. Named lifetimes allow | ||
for grouping of parameters by lifetime. For example, consider this | ||
function: | ||
Lifetimes can be named and referenced. For example, the special lifetime | ||
`'static`, which does not go out of scope, can be used to create global | ||
variables and communicate between tasks (see the manual for usecases). | ||
|
||
## Parameter Lifetimes | ||
|
||
Named lifetimes allow for grouping of parameters by lifetime. | ||
For example, consider this function: | ||
|
||
~~~ | ||
# struct Point {x: f64, y: f64}; // as before | ||
|
@@ -655,6 +660,25 @@ fn select<'r, T>(shape: &Shape, threshold: f64, | |
|
||
This is equivalent to the previous definition. | ||
|
||
## Labeled Control Structures | ||
|
||
Named lifetime notation can also be used to control the flow of execution: | ||
|
||
~~~ | ||
'h: for i in range(0,10) { | ||
'g: loop { | ||
if i % 2 == 0 { continue 'h; } | ||
if i == 9 { break 'h; } | ||
break 'g; | ||
} | ||
} | ||
~~~ | ||
|
||
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. Could you expand this section and also mention that for-loop lables are hygienic? An example here: https://github.com/mozilla/rust/blob/master/src/test/run-pass/hygienic-labels.rs 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. @flaper87 Cool, had to read on that subject, hope I didn't make any mistakes. (Added it to the macros guide.) |
||
> ***Note:*** Labelled breaks are not currently supported within `while` loops. | ||
|
||
Named labels are hygienic and can be used safely within macros. | ||
See the macros guide section on hygiene for more details. | ||
|
||
# Conclusion | ||
|
||
So there you have it: a (relatively) brief tour of the lifetime | ||
|
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.
We currently don't have support for labels on
while
loops. We should probably mention that this just works onfor
loops. #12643