From cc57675516684eacc05831e692c4fc889d8bc8cb Mon Sep 17 00:00:00 2001 From: Clark Walker <303walker@gmail.com> Date: Tue, 19 Sep 2023 13:20:33 -0600 Subject: [PATCH] Update while_let.md: address inconsistent use of fn main between 2 compared code examples The original document compares 2 code blocks to illustrate the benefits of while let. The first example **is not** placed in a main function while the second example **is** placed in a main function. The problem: This inconsistency is significant because, among other points of comparison, they are being compared in terms of their level of indentation. Therefore... Placing one example in a main function and not the other presents a visually unequal basis of comparison. The solution I propose: is to just remove the main function in the second block since removing it reduces visual complexity. I think adding in a main function to the first block and keeping the main function in the second block is also a good option. IMO consistency between the examples is what's most important. --- src/flow_control/while_let.md | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/flow_control/while_let.md b/src/flow_control/while_let.md index 897375a8e2..745b7ae75f 100644 --- a/src/flow_control/while_let.md +++ b/src/flow_control/while_let.md @@ -31,26 +31,24 @@ loop { Using `while let` makes this sequence much nicer: ```rust,editable -fn main() { - // Make `optional` of type `Option` - let mut optional = Some(0); - - // This reads: "while `let` destructures `optional` into - // `Some(i)`, evaluate the block (`{}`). Else `break`. - while let Some(i) = optional { - if i > 9 { - println!("Greater than 9, quit!"); - optional = None; - } else { - println!("`i` is `{:?}`. Try again.", i); - optional = Some(i + 1); - } - // ^ Less rightward drift and doesn't require - // explicitly handling the failing case. +// Make `optional` of type `Option` +let mut optional = Some(0); + +// This reads: "while `let` destructures `optional` into +// `Some(i)`, evaluate the block (`{}`). Else `break`. +while let Some(i) = optional { + if i > 9 { + println!("Greater than 9, quit!"); + optional = None; + } else { + println!("`i` is `{:?}`. Try again.", i); + optional = Some(i + 1); } - // ^ `if let` had additional optional `else`/`else if` - // clauses. `while let` does not have these. + // ^ Less rightward drift and doesn't require + // explicitly handling the failing case. } +// ^ `if let` had additional optional `else`/`else if` +// clauses. `while let` does not have these. ``` ### See also: