3 ms·
How does this differ from the if-let syntax we already have? I'm a bit of a rust novice.
by 63 4y ago
How does this differ from the if-let syntax we already have? I'm a bit of a rust novice.
- kevincox 4y agoWith if-let you still need to "pass down" the variable. It would look like: let foo = if let Ok(f) = do_something() { f } else { // Do some error handling, maybe log a warning, maybe skip this item and continue... }; It's similar to `let else` but you still need this extra `f` variable that isn't really adding clarity.
- deleted 4y ago[deleted]
- ragnese 4y agoIt's mostly about avoiding nesting. Honestly, I'm still pretty ambivalent about it. But, before you might do: if let Some(x) = maybe_x { // do stuff with x } else { return; } now, you'd do: let Some(x) = maybe_x else { return; } // do stuff with x If you have several instances of this kind of thing, the nesting can be pretty ugly and hard to read, so we'd sometimes do janky things to avoid the nesting, like: let x = if let Some(x) = maybe_x { x } else { return; }; or, if let (Some(x), Some(y)) = (maybe_x, maybe_y) { // do stuff with x and y with only one layer of nesting } else { return; }
- __s 4y agoIt doesn't differ by much, here's an example of let-else replacing if-let: https://github.com/serprex/NULL/commit/9ab765c5089ce1f0e32cee78b191d32044c30b25 https://github.com/serprex/NULL/commit/9ab765c5089ce1f0e32ce... In another codebase I had something like let msg = if let Ok(msg) = ... { msg } else { continue }; becoming let Ok(msg) = ... else { continue };