Skip to content

Commit 4b224af

Browse files
author
noam
committed
Added suggested notes
* Note on while loop not supporting named breaks. * Note on hygienic macros (and example of such within loops)
1 parent 7dfa4b2 commit 4b224af

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/doc/guide-lifetimes.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,13 @@ points at a static constant).
559559

560560
# Named lifetimes
561561

562-
Lifetimes can be named and referenced. For example, the special lifetime
562+
Lifetimes can be named and referenced. For example, the special lifetime
563563
`'static`, which does not go out of scope, can be used to create global
564564
variables and communicate between tasks (see the manual for usecases).
565565

566566
## Parameter Lifetimes
567567

568-
Named lifetimes allow for grouping of parameters by lifetime.
568+
Named lifetimes allow for grouping of parameters by lifetime.
569569
For example, consider this function:
570570

571571
~~~
@@ -674,6 +674,11 @@ Named lifetime notation can also be used to control the flow of execution:
674674
}
675675
~~~
676676

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+
677682
# Conclusion
678683

679684
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

0 commit comments

Comments
 (0)