Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove nesting from let else example #2600

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/pattern-matching/let-control-flow/let-else.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ off the end of the block).

```rust,editable
fn hex_or_die_trying(maybe_string: Option<String>) -> Result<u32, String> {
// TODO: The structure of this code is difficult to follow -- rewrite it with let-else!
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 {
Err(String::from("not a hex digit"))
}
} else {
Err(String::from("got empty string"))
}
} else {
Err(String::from("got None"))
let s = match maybe_string {
Some(s) => s,
None => return Err(String::from("got None")),
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we strike a balance here:

Suggested change
let s = match maybe_string {
Some(s) => s,
None => return Err(String::from("got None")),
};
let s = if let Some(s) = maybe_string {
s
} else {
return Err(String::from("got None")),
};

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this helps? Using if let spreads it across more lines vertically, but doesn't demonstrate any more nesting. I'd say using match here is clearer, but I guess I don't feel that strongly about it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fundamentally, the thing that people get into here is "I need to pattern-match some enums, with an early exit if they don't match". I think the let .. = if .. else { return .. }; is a clearer representation of this desire than the match, since match doesn't represent the "else" as clearly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since match doesn't represent the "else" as clearly.

... isn't the None case the same as the else case? Are you saying that the None case in a match might be less clear to students than else is, since they'd be more familiar with if else coming from other languages?

Copy link
Collaborator Author

@randomPoison randomPoison Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I guess the change from if else to let else might also be a more direct transformation than going from match to let else?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm saying both of those things :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've changed it to use if let instead. @fw-immunant does this seem any better to you? The starting code still doesn't have any nesting, but we're now using if let instead of match, which maybe helps address your point about the motivation for let else?


let first_byte_char = match s.chars().next() {
Some(first) => first,
None => return Err(String::from("got empty string")),
};

match first_byte_char.to_digit(16) {
Some(digit) => Ok(digit),
None => Err(String::from("not a hex digit")),
}
}

Expand All @@ -29,11 +29,6 @@ fn main() {
```

<details>

`if-let`s can pile up, as shown. The `let-else` construct supports flattening
this nested code. Rewrite the awkward version for students, so they can see the
transformation.

The rewritten version is:

```rust
Expand All @@ -54,4 +49,12 @@ fn hex_or_die_trying(maybe_string: Option<String>) -> Result<u32, String> {
}
```

## More to Explore

- This early return-based control flow is common in Rust error handling code,
where you try to get a value out of a `Result`, returning an error if the
`Result` was `Err`.
- If students ask, you can also demonstrate how real error handling code would
be written with `?`.

</details>