Skip to content

Commit

Permalink
Added suggested notes
Browse files Browse the repository at this point in the history
* Note on while loop not supporting named breaks.
* Note on hygienic macros (and example of such within loops)
  • Loading branch information
noam committed Mar 24, 2014
1 parent 7dfa4b2 commit 4b224af
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/doc/guide-lifetimes.md
Expand Up @@ -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:

~~~
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/doc/guide-macros.md
Expand Up @@ -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
Expand Down

13 comments on commit 4b224af

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at noam87@4b224af

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging CLUSTERfoo/rust/docs/labelled_breaks = 4b224af into auto

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLUSTERfoo/rust/docs/labelled_breaks = 4b224af merged ok, testing candidate = 085f08dc

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at noam87@4b224af

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging CLUSTERfoo/rust/docs/labelled_breaks = 4b224af into auto

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLUSTERfoo/rust/docs/labelled_breaks = 4b224af merged ok, testing candidate = d8197f5a

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 25, 2014

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at noam87@4b224af

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging CLUSTERfoo/rust/docs/labelled_breaks = 4b224af into auto

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLUSTERfoo/rust/docs/labelled_breaks = 4b224af merged ok, testing candidate = e28f081

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 4b224af Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = e28f081

Please sign in to comment.