1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-12 12:18:06 +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:
Frances Wingerter 2023-07-06 17:23:02 +00:00 committed by GitHub
parent bd98a829c6
commit dd9047126c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

View File

@ -1,7 +1,8 @@
# Blocks
A block in Rust has a value and a type: the value is the last expression of the
block:
A block in Rust contains a sequence of expressions.
Each block has a value and a type,
which are those of the last expression of the block:
```rust,editable
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
return value:
@ -35,8 +38,6 @@ fn main() {
}
```
However if the last expression ends with `;`, then the resulting value and type is `()`.
<details>
Key Points:

View File

@ -23,7 +23,7 @@ Rust.
* 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`.
* 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
fn main() {