• smiletolerantly@awful.systems
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    4 days ago

    Drop if-let

    Over my cold dead body. if-let-else is such a fantastic pattern. Makes everything an order of magnitude more readable. Works so nicely for unwrapping opts/errs.

    • TehPers@beehaw.org
      link
      fedilink
      English
      arrow-up
      1
      ·
      4 days ago

      Their suggested replacement is closer to C#'s is keyword:

      if foo is Some(foo) {
          // ...
      }
      

      The issue here is that we still have let-else, which can’t be translated as easily:

      let Some(foo) = foo else {
          todo!();
      };
      
        • TehPers@beehaw.org
          link
          fedilink
          English
          arrow-up
          1
          ·
          4 days ago

          That’s not an alternative, it’s removing let-else entirely. It’s equivalent to this:

          let i = if let Some(i) = opt_number {
              i
          } else {
              return 0;
          };
          

          let-else is specifically a feature that allows you to use a refutable pattern to deconstruct a value by providing a diverging else branch.

          • soc@programming.dev
            link
            fedilink
            English
            arrow-up
            1
            ·
            edit-2
            3 days ago

            Removing let-else is the whole point of the linked article series:

            Being able to do to everything Rust does, but without Rust’s zoo of if-then-else, match, if-let, let-else etc.