Skip to content

Commit 7dfa4b2

Browse files
author
noam
committed
docs: named lifetimes
* Include tip given by Leo Testard in mailing list about labeled `break` and `continue`: https://mail.mozilla.org/pipermail/rust-dev/2014-March/009145.html * cross-reference named lifetimes in tutorial -> lifetimes guide * Broke named lifetimes section into two sub-sections. * Added mention of `'static` lifetime.
1 parent 903e838 commit 7dfa4b2

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/doc/guide-lifetimes.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,14 @@ points at a static constant).
559559

560560
# Named lifetimes
561561

562-
Let's look at named lifetimes in more detail. Named lifetimes allow
563-
for grouping of parameters by lifetime. For example, consider this
564-
function:
562+
Lifetimes can be named and referenced. For example, the special lifetime
563+
`'static`, which does not go out of scope, can be used to create global
564+
variables and communicate between tasks (see the manual for usecases).
565+
566+
## Parameter Lifetimes
567+
568+
Named lifetimes allow for grouping of parameters by lifetime.
569+
For example, consider this function:
565570

566571
~~~
567572
# struct Point {x: f64, y: f64}; // as before
@@ -655,6 +660,20 @@ fn select<'r, T>(shape: &Shape, threshold: f64,
655660

656661
This is equivalent to the previous definition.
657662

663+
## Labeled Control Structures
664+
665+
Named lifetime notation can also be used to control the flow of execution:
666+
667+
~~~
668+
'h: for i in range(0,10) {
669+
'g: loop {
670+
if i % 2 == 0 { continue 'h; }
671+
if i == 9 { break 'h; }
672+
break 'g;
673+
}
674+
}
675+
~~~
676+
658677
# Conclusion
659678

660679
So there you have it: a (relatively) brief tour of the lifetime

src/doc/tutorial.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,8 @@ a `&T` pointer. `MutexArc` is an example of a *sharable* type with internal muta
21032103
These are types that do not contain any data whose lifetime is bound to
21042104
a particular stack frame. These are types that do not contain any
21052105
references, or types where the only contained references
2106-
have the `'static` lifetime.
2106+
have the `'static` lifetime. (For more on named lifetimes and their uses,
2107+
see the [references and lifetimes guide][lifetimes].)
21072108
21082109
> ***Note:*** These two traits were referred to as 'kinds' in earlier
21092110
> iterations of the language, and often still are.

0 commit comments

Comments
 (0)