diff options
| author | noam <noam@clusterfoo.com> | 2014-03-23 21:24:17 -0400 |
|---|---|---|
| committer | noam <noam@clusterfoo.com> | 2014-03-24 00:43:43 -0400 |
| commit | 4b224af72a76770694dc0998b356d9ce4d77529b (patch) | |
| tree | ef9a2f55632ec72100b11d06798901f456109790 | |
| parent | 7dfa4b298262fde037b5fbbb8fe20468d7306cbb (diff) | |
| download | rust-4b224af72a76770694dc0998b356d9ce4d77529b.tar.gz rust-4b224af72a76770694dc0998b356d9ce4d77529b.zip | |
Added suggested notes
* Note on while loop not supporting named breaks. * Note on hygienic macros (and example of such within loops)
| -rw-r--r-- | src/doc/guide-lifetimes.md | 9 | ||||
| -rw-r--r-- | src/doc/guide-macros.md | 32 |
2 files changed, 39 insertions, 2 deletions
diff --git a/src/doc/guide-lifetimes.md b/src/doc/guide-lifetimes.md index 20268ce8bbe..eb16f73c3a9 100644 --- a/src/doc/guide-lifetimes.md +++ b/src/doc/guide-lifetimes.md @@ -559,13 +559,13 @@ points at a static constant). # Named lifetimes -Lifetimes can be named and referenced. For example, the special lifetime +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. +Named lifetimes allow for grouping of parameters by lifetime. For example, consider this function: ~~~ @@ -674,6 +674,11 @@ Named lifetime notation can also be used to control the flow of execution: } ~~~ +> ***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 diff --git a/src/doc/guide-macros.md b/src/doc/guide-macros.md index 527777a0eba..23510997f24 100644 --- a/src/doc/guide-macros.md +++ b/src/doc/guide-macros.md @@ -398,6 +398,38 @@ position (in particular, not as an argument to yet another macro invocation), the expander will then proceed to evaluate `m2!()` (along with any other macro invocations `m1!(m2!())` produced). +# Hygiene + +To prevent clashes, rust implements +[hygienic macros](http://en.wikipedia.org/wiki/Hygienic_macro). + +As an example, `loop` and `for-loop` labels (discussed in the lifetimes guide) +will not clash. The following code will print "Hello!" only once: + +~~~ +#[feature(macro_rules)]; + +macro_rules! loop_x ( + ($e: expr) => ( + // $e will not interact with this 'x + 'x: loop { + println!("Hello!"); + $e + } + ); +) + +fn main() { + 'x: loop { + loop_x!(break 'x); + println!("I am never printed."); + } +} +~~~ + +The two `'x` names did not clash, which would have caused the loop +to print "I am never printed" and to run forever. + # A final note Macros, as currently implemented, are not for the faint of heart. Even |
