-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
The RFC introducing |
Talking about reducing rightward drift makes sense as a motivation for
I get what you're saying about demonstrating the norms in rust around de-nesting and early returns, but I think that's less important than getting students to understand what |
let s = match maybe_string { | ||
Some(s) => s, | ||
None => return Err(String::from("got None")), | ||
}; |
There was a problem hiding this comment.
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:
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")), | |
}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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
?
I think focusing on deeply nested control on this slide distracts from the main point of the slide. The slide frames
let else
as the solution to deep nesting, when it's really the early returns that allow for the simplification (at least in this example). This makes it harder to explainlet else
to students because we're simultaneously changing the control flow structure and introducing a new language construct. I think if we started with amatch
-based example that's already using early returns it'd be clearer to students howlet else
can be a more concise version ofmatch
in cases like this.If this seems like a bad idea to others I can change this to move the
match
-based version into the speaker notes. That'd at least make it easier to first show the nested code, then an unnested version usingmatch
, and then finally an unnested version usinglet else
.