Skip to content

Add the loop_break_value feature to the reference #53

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

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 14 additions & 1 deletion src/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ Significantly, lambda expressions _capture their environment_, which regular
[function definitions](items.html#functions) do not. The exact type of capture
depends on the [function type](types.html#function-types) inferred for the
lambda expression. In the simplest and least-expensive form (analogous to a
```|| { }``` expression), the lambda expression captures its environment by
`|| { }` expression), the lambda expression captures its environment by
reference, effectively borrowing pointers to all outer variables mentioned
inside the function. Alternately, the compiler may infer that a lambda
expression should copy or move values (depending on their type) from the
Expand Down Expand Up @@ -600,6 +600,11 @@ within this loop may exit out of this loop or return control to its head.
See [break expressions](#break-expressions) and [continue
expressions](#continue-expressions).

A `loop` expression is [diverging](items.html#diverging-functions), and doesn't
return anything. However, when it contains a [break
expression](#break-expressions), it returns the value attached to the `break`
that caused the loop to stop.

## `break` expressions

A `break` expression has an optional _label_. If the label is absent, then
Expand All @@ -608,6 +613,14 @@ enclosing it. It is only permitted in the body of a loop. If the label is
present, then `break 'foo` terminates the loop with label `'foo`, which need not
be the innermost label enclosing the `break` expression, but must enclose it.

When the `break` expression is enclosed in a `loop` it has an optional return
value attached which will be returned by the loop ended by the
expression. When it's not provided it defaults to `()`.

If both a label and a return value are present in the expression, the label
must be placed before the return value. For example `break 'label 42` breaks
the loop labelled `'label`, returning `42` from it.

## `continue` expressions

A `continue` expression has an optional _label_. If the label is absent, then
Expand Down