top | item 46331977 (no title) Rucadi | 2 months ago This is also somewhat common in c++ with immediate-invoked lambdas discuss order hn newest jeroenhd|2 months ago The same pattern can also be useful in Rust for early returning Result<_,_> errors (you cannot `let x = foo()?` inside of a normal block like that). let config: Result<i32, i32> = { Ok( "1234".parse::<i32>().map_err(|_| -1)? ) }; would fail to compile, or worse: would return out of the entire method if surrounding method would have return type Result<_,i32>. On the other hand, let config: Result<i32, i32> = (||{ Ok( "1234".parse::<i32>().map_err(|_| -1)? ) })(); runs just fine.Hopefully try blocks will allow using ? inside of expression blocks in the future, though. carstimon|2 months ago A blog post for it from a prominent c++er https://herbsutter.com/2013/04/05/complex-initialization-for... knorker|2 months ago Yeah but languages that make you resort to this then don't let you simply return from the block.And the workarounds often make the pattern be a net loss in clarity.
jeroenhd|2 months ago The same pattern can also be useful in Rust for early returning Result<_,_> errors (you cannot `let x = foo()?` inside of a normal block like that). let config: Result<i32, i32> = { Ok( "1234".parse::<i32>().map_err(|_| -1)? ) }; would fail to compile, or worse: would return out of the entire method if surrounding method would have return type Result<_,i32>. On the other hand, let config: Result<i32, i32> = (||{ Ok( "1234".parse::<i32>().map_err(|_| -1)? ) })(); runs just fine.Hopefully try blocks will allow using ? inside of expression blocks in the future, though.
carstimon|2 months ago A blog post for it from a prominent c++er https://herbsutter.com/2013/04/05/complex-initialization-for...
knorker|2 months ago Yeah but languages that make you resort to this then don't let you simply return from the block.And the workarounds often make the pattern be a net loss in clarity.
jeroenhd|2 months ago
Hopefully try blocks will allow using ? inside of expression blocks in the future, though.
carstimon|2 months ago
knorker|2 months ago
And the workarounds often make the pattern be a net loss in clarity.