logoalt Hacker News

ggirelliyesterday at 7:01 PM1 replyview on HN

Loved the article, such a nice read. I am still slowly ramping up my proficiency in Rust and this gave me a lot of things to think through. I particularly enjoyed the temporary mutability pattern, very cool and didn't think about it before!


Replies

aw1621107yesterday at 7:35 PM

> I particularly enjoyed the temporary mutability pattern, very cool and didn't think about it before!

It's not too uncommon in other languages (sometimes under the name "immediately invoked function expression"), though depending on the language you may see lambdas involved. For example, here's one of the examples from the article ported to C++:

    auto data = []() {
        auto data = get_vector();
        auto temp = compute_something();
        data.insert_range(data.end(), temp);
        std::ranges::sort(data);
        return data;
    }();