You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Often when teaching people are underwhelmed by the utility of the let...else construct. I also don't think it makes a huge amount of sense as presented. The current example consists of several repetitions of the admittedly awkward pattern
let s = ifletSome(s) = maybe_string {
s
}else{returnErr(String::from("got None"));};
which is improved by let...else, but is also not a terribly common pattern on its own, and as I understand things isn't really what motivated this control flow construct.
My understanding is that the real utility of let...else is that it allows to de-nest what would otherwise be a rightward-drifting nesting of if lets:
ifletSome(x) = obtain_x(){ifletSome(y) = x.obtain_y(){iflet z = compute_z(x, y){// work with z}}}
Which can instead become:
letSome(x) = obtain_x()else{ return }letSome(y) = x.obtain_y()else{ return }letSome(z) = compute_z(x, y)else{ return }// work with z
I think this makes the utility of the construct more clear.
The text was updated successfully, but these errors were encountered:
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
Often when teaching people are underwhelmed by the utility of the
let
...else
construct. I also don't think it makes a huge amount of sense as presented. The current example consists of several repetitions of the admittedly awkward patternwhich is improved by
let
...else
, but is also not a terribly common pattern on its own, and as I understand things isn't really what motivated this control flow construct.My understanding is that the real utility of
let
...else
is that it allows to de-nest what would otherwise be a rightward-drifting nesting ofif let
s:Which can instead become:
I think this makes the utility of the construct more clear.
The text was updated successfully, but these errors were encountered: