Skip to content

Commit e28f081

Browse files
committed
auto merge of #13106 : CLUSTERfoo/rust/docs/labelled_breaks, r=brson
* 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.
2 parents 6053102 + 4b224af commit e28f081

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

src/doc/guide-lifetimes.md

Lines changed: 27 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,25 @@ 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+
677+
> ***Note:*** Labelled breaks are not currently supported within `while` loops.
678+
679+
Named labels are hygienic and can be used safely within macros.
680+
See the macros guide section on hygiene for more details.
681+
658682
# Conclusion
659683

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

src/doc/guide-macros.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,38 @@ position (in particular, not as an argument to yet another macro invocation),
398398
the expander will then proceed to evaluate `m2!()` (along with any other macro
399399
invocations `m1!(m2!())` produced).
400400

401+
# Hygiene
402+
403+
To prevent clashes, rust implements
404+
[hygienic macros](http://en.wikipedia.org/wiki/Hygienic_macro).
405+
406+
As an example, `loop` and `for-loop` labels (discussed in the lifetimes guide)
407+
will not clash. The following code will print "Hello!" only once:
408+
409+
~~~
410+
#[feature(macro_rules)];
411+
412+
macro_rules! loop_x (
413+
($e: expr) => (
414+
// $e will not interact with this 'x
415+
'x: loop {
416+
println!("Hello!");
417+
$e
418+
}
419+
);
420+
)
421+
422+
fn main() {
423+
'x: loop {
424+
loop_x!(break 'x);
425+
println!("I am never printed.");
426+
}
427+
}
428+
~~~
429+
430+
The two `'x` names did not clash, which would have caused the loop
431+
to print "I am never printed" and to run forever.
432+
401433
# A final note
402434

403435
Macros, as currently implemented, are not for the faint of heart. Even

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)