-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
265 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
minutes: 5 | ||
--- | ||
|
||
# Conditionals | ||
|
||
Much of the Rust syntax will be familiar to you from C, C++ or Java: | ||
|
||
* Blocks are delimited by curly braces. | ||
* Line comments are started with `//`, block comments are delimited by `/* ... | ||
*/`. | ||
* Keywords like `if` and `while` work the same. | ||
* Variable assignment is done with `=`, comparison is done with `==`. | ||
|
||
## `if` expressions | ||
|
||
You use [`if` | ||
expressions](https://doc.rust-lang.org/reference/expressions/if-expr.html#if-expressions) | ||
exactly like `if` statements in other languages: | ||
|
||
```rust,editable | ||
fn main() { | ||
let x = 10; | ||
if x < 20 { | ||
println!("small"); | ||
} else if x < 100 { | ||
println!("biggish"); | ||
} else { | ||
println!("huge"); | ||
} | ||
} | ||
``` | ||
|
||
In addition, you can use `if` as an expression. The last expression of each | ||
block becomes the value of the `if` expression: | ||
|
||
|
||
```rust,editable | ||
fn main() { | ||
let x = 10; | ||
let size = if x < 20 { | ||
"small" | ||
} else { | ||
"large" | ||
}; | ||
println!("number size: {}", size); | ||
} | ||
``` | ||
|
||
<details> | ||
|
||
Because `if` is an expression and must have a particular type, both of its branch blocks must have the same type. Show what happens if you add `;` after `"small"` in the second example. | ||
|
||
When `if` is used in an expression, the expression must have a `;` to separate | ||
it from the next statement. Remove the `;` before `println!` to see the compiler | ||
error. | ||
|
||
</details> |
Oops, something went wrong.