• TehPers@beehaw.org
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    1 year ago

    Also worth mentioning, but you can early-return from a block in rust too, just using the break keyword and named blocks:

    let x = 'my_block {
        if thing() { break 'my_block 1; }
        2
    };
    

    Edit: I haven’t tried this with async blocks, but I’m guessing it works there too?

    • nous@programming.dev
      link
      fedilink
      English
      arrow-up
      3
      ·
      1 year ago

      Flow control like return and break work differently in async blocks. They are much closer to closures than blocks in this regard. And need to be as they are lazily evaluated (like closures) and may even be evaluated well after the code that contains them has finished.

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

        Looks like labels don’t work on async blocks, but using a nested inner block does work. Also, yeah, async blocks only exist to create Futures, they don’t execute until you start polling them. I don’t really see any reason why you couldn’t stick a label on the async block itself though, breaking would at worst just create one new state for the future - early returned.