4 ms·
Match ergonomics was one of the greatest improvements to the language, I’d personally put it almost on par with NLL. For anyone who’s interested: In Rust, you
by codeflo 5y ago
Match ergonomics was one of the greatest improvements to the language, I’d personally put it almost on par with NLL.
For anyone who’s interested:
In Rust, you tend to do a lot of pattern matching and destructuring. But you also often only have a reference to your data, because copies can be expensive.
Combining pattern matching and references was always possible, but confusing: you had to place lots of “ref” annotations in various places, which I never got right on the first try. But now, you can magically destructure e.g. a reference to a tuple into a tuple of references. You don’t have to think about it, it simply works. Combined with Rust’s algebraic types, this allows you to write very elegant, almost fp-like code that’s also zero copy.
- thomasahle 5y agoThose match "ergonomics" really tripped me up when first trying to learn Rust. I was trying to figure out references and deref, and I kept thinking I "got it" and trying to make sure by writing examples that should and shouldn't work. However, because of all the deref "ergonomics" magically spread around thinks like match, it was really hard to confirm whether I had the right mental model or not. Lots of things that surely shouldn't work somehow worked, and I couldn't explain why. I kept having to go to forums and people just told me "oh, this is a special magic case we added". Very frustrating.
- masklinn 5y agoYes, while "match ergonomics" made many "just work", the magic it implies also obscures the language, and code, a lot. I remain of two minds about it. If you want to make sure you have the right model you can always set edition to 2015 tho. You will also lose NLL and a bunch of other conveniences, but match ergonomics will be disabled.
- steveklabnik 5y agoNLL is in Rust 2015 these days. (As of Rust 1.36.)
- thomasahle 5y ago> If you want to make sure you have the right model you can always set edition to 2015 tho. That's a really interesting idea. I wonder if that would work well for teaching languages in general. Like teaching Java 1 instead of 16 where the language was much simpler.
- codeflo 5y agoI can totally see how this kind of convenience might hinder learning. For me, the fact that “&a” does so much magic type conversion stuff had a similar effect. One thing I sometimes find helpful is to run an IDE (like VS Code with the Rust Analyzer plugin) and hover over every single variable to see its type. Out of curiosity, since I expect to give a short Rust introduction in the near future, what was your language background when learning Rust? How familiar were you with C-style pointers and/or C++-style references?
- iudqnolq 5y agoA similar trick I learned from Jon Gjenset is to assert something has the unit type and read the error. let foo: () = complex_thingy();
- thomasahle 5y ago> Out of curiosity, since I expect to give a short Rust introduction in the near future, what was your language background when learning Rust? I have a background in lots of high level languages, like Python and Haskell, but never thought too much about how the types were represented. But prior to learning Rust I spent some time writing C (I've never done C++), so I guess I came in with a simple idea of how pointers work.