logoalt Hacker News

loegyesterday at 1:02 AM3 repliesview on HN

Can this just be done as a lambda that is immediately evaluated? It's just much more verbose.

    let x = (|| -> Result<i32, std::num::ParseIntError> {
         Ok("1".parse::<i32>()?
          + "2".parse::<i32>()?
          + "3".parse::<i32>()?)
    })();

Replies

rendawyesterday at 4:23 AM

That prevents other control flow mechanisms (return, break) from operating past the function boundary. In general, I avoid single-callsite functions as much as possible (including the iterator api) for this reason.

show 1 reply
schneemsyesterday at 5:56 AM

Wouldn't that also move any referenced variables too? Unlike the block example that would make this code not identical to what it's replacing.

show 1 reply
saghmyesterday at 3:27 AM

My instinct is this would get hairy much faster if you want to actually close over variables compared to using a block.

show 2 replies