mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-20 15:08:02 +02:00
A few improvements to control flow section (#907)
* control-flow: blocks: clarify prose around block values/types specify what determines the type of a block move the last expression note to the discussion of blocks rather than functions to clarify that it applies to both * control-flow: if-let: correct sense of 'non-returning'
This commit is contained in:
parent
bd98a829c6
commit
dd9047126c
@ -1,7 +1,8 @@
|
|||||||
# Blocks
|
# Blocks
|
||||||
|
|
||||||
A block in Rust has a value and a type: the value is the last expression of the
|
A block in Rust contains a sequence of expressions.
|
||||||
block:
|
Each block has a value and a type,
|
||||||
|
which are those of the last expression of the block:
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -22,6 +23,8 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If the last expression ends with `;`, then the resulting value and type is `()`.
|
||||||
|
|
||||||
The same rule is used for functions: the value of the function body is the
|
The same rule is used for functions: the value of the function body is the
|
||||||
return value:
|
return value:
|
||||||
|
|
||||||
@ -35,8 +38,6 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
However if the last expression ends with `;`, then the resulting value and type is `()`.
|
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
|
||||||
Key Points:
|
Key Points:
|
||||||
|
@ -23,7 +23,7 @@ Rust.
|
|||||||
* Unlike `match`, `if let` does not have to cover all branches. This can make it more concise than `match`.
|
* Unlike `match`, `if let` does not have to cover all branches. This can make it more concise than `match`.
|
||||||
* A common usage is handling `Some` values when working with `Option`.
|
* A common usage is handling `Some` values when working with `Option`.
|
||||||
* Unlike `match`, `if let` does not support guard clauses for pattern matching.
|
* Unlike `match`, `if let` does not support guard clauses for pattern matching.
|
||||||
* Since 1.65, a similar [let-else](https://doc.rust-lang.org/rust-by-example/flow_control/let_else.html) construct allows to do a destructuring assignment, or if it fails, have a non-returning block branch (panic/return/break/continue):
|
* Since 1.65, a similar [let-else](https://doc.rust-lang.org/rust-by-example/flow_control/let_else.html) construct allows to do a destructuring assignment, or if it fails, execute a block which is required to abort normal control flow (with `panic`/`return`/`break`/`continue`):
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user