Live data from Hacker News

Rust's Block Pattern

notgull.net

31–40 of 121 posts

Re: Rust's Block Pattern

#31

I love that this is part of the syntax. I typically use closures to do this in other languages, but the syntax is always so cumbersome. You get the "dog balls" that Douglas Crockford always called them: ``` const config = (() => { const raw_data = ... ... return compiled; })()' const result = config.whatever; // carry on return result; ``` Really wish block were expressions in more languages.

Yes, I constantly use this pattern in C++/JavaScript, although I haven't tested how performant it is in the former (what does the compiler even do with such an expression?)

Re: Rust's Block Pattern

#33

GCC adds similar syntax as an extension to C: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html It's used all throughout the Linux kernel and useful for macros.

The best part of statement expressions is that a return there returns from the function itself, not from the statement expr.

I use that with with macros to return akins to std::expected, while maintaining the code in the happy-path like with exceptions.

Re: Rust's Block Pattern

#35
post #28

I have one better: the try block pattern. https://doc.rust-lang.org/beta/unstable-book/language-featur...

Why does this need special syntax? Couldn't blocks do this if the expression returns a result in the end?

Try blocks let you encapsulate the early-return behavior of Try-returning operations so that they don't leak through to the surrounding function. This lets you use the ? operator 1. when the Try type doesn't match that of the function this is taking place in 2. when you want to use ? to short circuit, but don't want to return from the enclosing function. For instance, in a function returning Result, you could have a try block where you do a bunch of operations with Option and make use of the ? operator, or have ? produce an Err without returning from the enclosing function. Without try blocks, you pretty much need to define a one-off closure or function so that you can isolate the use of ? within its body.

Re: Rust's Block Pattern

#36
post #28

I have one better: the try block pattern. https://doc.rust-lang.org/beta/unstable-book/language-featur...

Why does this need special syntax? Couldn't blocks do this if the expression returns a result in the end?

The best part of try blocks is the ability to use the ? operator within them. Any block can return a result, but only function blocks (and try blocks) can propagate an Err with the ? operator.

Re: Rust's Block Pattern

#37

You can also de-mut-ify a variable by simply shadowing it with an immutable version of itself: let mut data = foo(); data.mutate(); let data = data; May be preferable for short snippets where adding braces, the yielded expression, and indentation is more noise than it's worth.

Variable shadowing felt wrong for a while because it's considered verboten in so many other environments. I use it fairly liberally in rust now.

Re: Rust's Block Pattern

#39
I use this all the time. It's features like these that sell Rust for me honestly; even if you wrapped your whole program in `unsafe` it would still be a massively better language than C++ or C.
Post reply on HN