From dd9047126ccd2ca05f14acc87a5f101f383a3b36 Mon Sep 17 00:00:00 2001 From: Frances Wingerter <91758128+fw-immunant@users.noreply.github.com> Date: Thu, 6 Jul 2023 17:23:02 +0000 Subject: [PATCH] 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' --- src/control-flow/blocks.md | 9 +++++---- src/control-flow/if-let-expressions.md | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/control-flow/blocks.md b/src/control-flow/blocks.md index f09f8bbf..c734837d 100644 --- a/src/control-flow/blocks.md +++ b/src/control-flow/blocks.md @@ -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 `()`. -
Key Points: diff --git a/src/control-flow/if-let-expressions.md b/src/control-flow/if-let-expressions.md index 62b00c3e..c84cc38c 100644 --- a/src/control-flow/if-let-expressions.md +++ b/src/control-flow/if-let-expressions.md @@ -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() {