mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-27 01:27:37 +02:00
I've taken some work by @fw-immunant and others on the new organization of the course and condensed it into a form amenable to a text editor and some computational analysis. You can see the inputs in `course.py` but the interesting bits are the output: `outline.md` and `slides.md`. The idea is to break the course into more, smaller segments with exercises at the ends and breaks in between. So `outline.md` lists the segments, their duration, and sums those durations up per-day. It shows we're about an hour too long right now! There are more details of the segments in `slides.md`, or you can see mostly the same stuff in `course.py`. This now contains all of the content from the v1 course, ensuring both that we've covered everything and that we'll have somewhere to redirect every page. Fixes #1082. Fixes #1465. --------- Co-authored-by: Nicole LeGare <dlegare.1001@gmail.com> Co-authored-by: Martin Geisler <mgeisler@google.com>
60 lines
1.3 KiB
Markdown
60 lines
1.3 KiB
Markdown
---
|
|
minutes: 5
|
|
---
|
|
|
|
# `break` and `continue`
|
|
|
|
If you want to exit any kind of loop early, use
|
|
[`break`](https://doc.rust-lang.org/reference/expressions/loop-expr.html#break-expressions).
|
|
For `loop`, this can take an optional expression that becomes the value of the `loop` expression.
|
|
|
|
If you want to immediately start
|
|
the next iteration use [`continue`](https://doc.rust-lang.org/reference/expressions/loop-expr.html#continue-expressions).
|
|
|
|
```rust,editable
|
|
fn main() {
|
|
let (mut a, mut b) = (100, 52);
|
|
let result = loop {
|
|
if a == b {
|
|
break a;
|
|
}
|
|
if a < b {
|
|
b -= a;
|
|
} else {
|
|
a -= b;
|
|
}
|
|
};
|
|
println!("{result}");
|
|
}
|
|
```
|
|
|
|
Both `continue` and `break` can optionally take a label argument which is used
|
|
to break out of nested loops:
|
|
|
|
```rust,editable
|
|
fn main() {
|
|
'outer: for x in 1..5 {
|
|
println!("x: {x}");
|
|
let mut i = 0;
|
|
while i < x {
|
|
println!("x: {x}, i: {i}");
|
|
i += 1;
|
|
if i == 3 {
|
|
break 'outer;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
In this case we break the outer loop after 3 iterations of the inner loop.
|
|
|
|
<details>
|
|
|
|
* Note that `loop` is the only looping construct which returns a non-trivial
|
|
value. This is because it's guaranteed to be entered at least once (unlike
|
|
`while` and `for` loops).
|
|
|
|
</details>
|
|
|