Rust is beautiful

I've invested some time to learn in detail about Rust, which means reading the excellent online book here. And it is beautiful. It makes me wish I could pause the world for a few years, to convert some 500,000 lines of C++ that exist under my purview into Rust, and continue from there.

Rust seems to take all the little design lessons I've learned in 20 years of C++ programming, and consolidates them into one language:
  • It's not best that everything is mutable by default, and const if the programmer points it out. It's healthier the other way around.
  • The fundamental string type is a sensible, immutable string slice (in Rust, a &str). This is great for zero-copy parsers, such as nom. Our code has had that for a decade – I named it Seq, or SeqPtr. C++ is adding std::string_view in C++17.
  • Elegant built-in variant with pattern-matching (in Rust, this is an enum). C++ is adding std::variant in C++17.
  • Type traits solve the problems of abstraction and generics, providing both static and dynamic dispatch, in an apparently more elegant manner than C++ inheritance (which is dynamic-only) or templates (which are static-only). Traits seem similar to concepts, which for now (unfortunately) remains a glimmer in Bjarne Stroustrup's eye.
  • Universal function call syntax. Something else Bjarne would like to introduce to C++.
  • Macros. Gawd, better macros (though not ideal – too templatey).
  • And of course, the crown – which sadly can't be brought to C++: compile-time memory safety!
If I were to start a programming career right now, I would use Rust. Hands down. I wish our major operating systems – let alone software we use – could be rewritten in it.

No comments: