1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-22 14:59:37 +02:00

Change let-else example to demonstrate undesirable nesting ()

Fixes .

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
This commit is contained in:
Frances Wingerter 2024-08-13 14:26:52 +00:00 committed by GitHub
parent da9f57a462
commit 6d743dac5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -42,22 +42,18 @@ off the end of the block).
```rust,editable
fn hex_or_die_trying(maybe_string: Option<String>) -> Result<u32, String> {
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"))
}
}