Confusing syntax of self

It is also the case that these things are actually really very simple and consistent within this system of notation:

  • self means self: Self
  • &self means self: &Self
  • &mut self means self: &mut Self

And as for lifetime elision, it's basically, "if there is a self, use its lifetime, otherwise the rest of the elision rules kick in".

This is a very straightforward transformation that anybody can get used to after basically a couple of days with the language. Like @jbe, I also like to think of self as a special case — because it really is a separate set of rules, albeit very minimal.


It is worth noting that any reasonably general-purpose language will have sub-languages, almost mini-EDSLs, inside it. It's pretty much impossible to practically, elegantly, concisely, and correctly notate conceptually distant ideas with the exact same syntax.

For example, there is a whole mini-EDSL for string formatting in Rust, but that's not only Rust; the tradition has lived on, from C through Python to JavaScript. It would make no sense to try and shove the very specific (and substantially simpler) semantics of string formatting into the same syntax as some other subset of the language. Consistency is great and should be one of the foremost goals of language design, but artificially mashing together different concepts can be just as bad as inventing ad-hoc notation for closely related concepts. (Cf. why I think that dot-postfix .await was a mistake.)

8 Likes