From 6d743dac5aa1da671b4cac6bcfa9cb068e2f9ff1 Mon Sep 17 00:00:00 2001 From: Frances Wingerter <91758128+fw-immunant@users.noreply.github.com> Date: Tue, 13 Aug 2024 14:26:52 +0000 Subject: [PATCH] Change let-else example to demonstrate undesirable nesting (#2276) Fixes #2070. Previously we showed a forcibly de-nested version using both let and if-let. this is not a construction that new learners of Rust are likely to have seen or written, while nesting if-let is closer to patterns that appear in other languages and better motivates the de-nesting transformation to let-else --- src/pattern-matching/let-control-flow.md | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/pattern-matching/let-control-flow.md b/src/pattern-matching/let-control-flow.md index 156d5a03..f563239a 100644 --- a/src/pattern-matching/let-control-flow.md +++ b/src/pattern-matching/let-control-flow.md @@ -42,22 +42,18 @@ off the end of the block). ```rust,editable fn hex_or_die_trying(maybe_string: Option) -> Result { - let s = if let Some(s) = maybe_string { - s + if let Some(s) = maybe_string { + if let Some(first_byte_char) = s.chars().next() { + if let Some(digit) = first_byte_char.to_digit(16) { + Ok(digit) + } else { + return Err(String::from("not a hex digit")); + } + } else { + return Err(String::from("got empty string")); + } } else { return Err(String::from("got None")); - }; - - let first_byte_char = if let Some(first_byte_char) = s.chars().next() { - first_byte_char - } else { - return Err(String::from("got empty string")); - }; - - if let Some(digit) = first_byte_char.to_digit(16) { - Ok(digit) - } else { - Err(String::from("not a hex digit")) } }