•  TehPers   ( @TehPers@beehaw.org ) 
    link
    fedilink
    4
    edit-2
    11 months 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   ( @nous@programming.dev ) 
      link
      fedilink
      English
      311 months 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.