by staticassertion · 7y ago · 0 comments
https://play.rust-lang.org/?version=stable&mode=debug&editio...
While the `entry` API provided an ergonomic, more performant way to do this, this is an example of a common pattern one might expect from other languages that, until recently, did not compile.
NLL and other ergonomics improvements have helped a lot with these paper-cuts.
*example_map.entry("some_key").or_insert(0) += 1;
Or, if you prefer an extra intermediate for clarity: let entry = example_map.entry("some_key").or_insert(0);
*entry += 1;
This is a good example of how, often, Rust’s approach of having greater underlying complexity but exposing it sanely ends up allowing you to write something that is semantically superior and easier to reason about, while being faster too.This and Rust’s ownership model (which I declare stands under the same banner) is the sort of thing that I regularly miss when working in JavaScript (and regularly missed when I used to work in Python plenty).