by codeflo · 5y ago · 0 comments
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.
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.
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.
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.
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?
let foo: () = complex_thingy();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.