Skip to content

docs: named lifetimes #13106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/doc/guide-lifetimes.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,14 @@ points at a static constant).

# Named lifetimes

Let's look at named lifetimes in more detail. Named lifetimes allow
for grouping of parameters by lifetime. For example, consider this
function:
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.
For example, consider this function:

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

This is equivalent to the previous definition.

## Labeled Control Structures

Named lifetime notation can also be used to control the flow of execution:
Copy link
Contributor

Choose a reason for hiding this comment

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

We currently don't have support for labels on while loops. We should probably mention that this just works on for loops. #12643


~~~
'h: for i in range(0,10) {
'g: loop {
if i % 2 == 0 { continue 'h; }
if i == 9 { break 'h; }
break 'g;
}
}
~~~

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you expand this section and also mention that for-loop lables are hygienic? An example here: https://github.com/mozilla/rust/blob/master/src/test/run-pass/hygienic-labels.rs

Copy link
Author

Choose a reason for hiding this comment

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

@flaper87 Cool, had to read on that subject, hope I didn't make any mistakes. (Added it to the macros guide.)

> ***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
Original file line number Diff line number Diff line change
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
3 changes: 2 additions & 1 deletion src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,8 @@ a `&T` pointer. `MutexArc` is an example of a *sharable* type with internal muta
These are types that do not contain any data whose lifetime is bound to
a particular stack frame. These are types that do not contain any
references, or types where the only contained references
have the `'static` lifetime.
have the `'static` lifetime. (For more on named lifetimes and their uses,
see the [references and lifetimes guide][lifetimes].)

> ***Note:*** These two traits were referred to as 'kinds' in earlier
> iterations of the language, and often still are.
Expand Down