I think the author misunderstood something....
Rust's Block Pattern
81–90 of 121 posts
Re: Rust's Block Pattern
#82Earlier quoted context omitted.
I want that stabilized so bad but it's not been really moving forward.
Out of curiosity why can’t a block just do this natively?
Re: Rust's Block Pattern
#83Earlier quoted context omitted.
I want that stabilized so bad but it's not been really moving forward.
Out of curiosity why can’t a block just do this natively?
Re: Rust's Block Pattern
#84More significantly the new variables x and y in the block are Drop'd at the end of the block rather than at the end of the function. This can be significant if: - Drop does something, like close a file or release a lock, or - x and y don't have Send and/or Sync, and you have an await point in the function or are doing multi-threaded stuff This is why you should almost always use std::sync::Mutex rather than tokio::sy…
Re: Rust's Block Pattern
#85Re: Rust's Block Pattern
#86Earlier quoted context omitted.
I just learned this one, and am gradually starting to use it! It applies for loops too. I saw it in ChatGPT code, and had to stop and look it up. Rust is a big language, for worse and for better.
I wouldn't call Rust "a big language" because of labeled break. This is a pretty standard language feature, you can do the same in C (and therefore C++), Go, Javascript, Java, C#...
Re: Rust's Block Pattern
#87This is also somewhat common in c++ with immediate-invoked lambdas
And the workarounds often make the pattern be a net loss in clarity.
Re: Rust's Block Pattern
#88I 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.
C++ lambdas can be used to achieve a similar result, not as pretty though https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines... But in general I agree!
Re: Rust's Block Pattern
#89Re: Rust's Block Pattern
#90I have one better: the try block pattern. https://doc.rust-lang.org/beta/unstable-book/language-featur...