Rust has been all the hype for some years now in the compiled space. As a new revision of this book is about to get out, I managed to get my hand on the first revision to see how I could learn Rust with it.
Discussion
Rust has started with very specific ideas in mind, compared to a language like C or C++. Safety is a problem that we all face. We don’t want to the program to crash and we don’t want to have cracks in which malware can get inside and steal data or crash the app. Rust tries to address some of these concerns, and as more and more people are using it for performance and sensitive applications (finance, even the Linux kernel maybe!), the language starts being quite mature.
As is usual with O’Reilly (if I’m not mistaken), the first relevant chapter is a quick tutorial with a few code example. First installing Rust (although I didn’t use dustup at all, just home-brew, although it may have been a mistake considering the fact that dustup seems to be used a lot in the Rust ecosystem), then showing up unit tests in Rust, all that to forget anywhere after that UT example. Still, very good to have UT presented from the start and it’s also a fundamental difference with the most of the other languages: testing is one of the core aspects of Rust.
We then move to basic types example, especially with arrays, vectors and strings. These last types are actually more complex that in most of other languages, especially since Rust doesn’t convert and is a very strictly typed language, and the book will add more layers on these types as concepts are presented. This is very nice as presenting all the concepts in one go would be overwhelming. We then move to the central ownership concepts, and with it the move semantic (which is very odd coming from a C++ guy where move semantics appeared years after he started learning the language).
Building on top, we have references, and that’s where the headache starts with Rust. It’s very nicely explained with all the different options that you can get, although it’s not nearly enough for me to be able to design a fully fledged app. But it’s normal, it’s only chapter 5! The concept of lifetime also adds complexity, but usually it seems that we don’t have to explicitly state lifetimes, so I guess it’s OK.
After the core foundation of Rust, we move to the different expressions in the language. To be fair, after that, you have all the tools to start writing in Rust. Definitely not good Rust but you have the basic building blocs. The rest of the book will go on with presenting the good practices and the tools in Rust to write good code, starting with error handling and modules.
Another odd aspect of Rust (to me, a guy used to traditional object oriented hierarchies) is how structs are handled and how you “inherit” functionality. It’s going to take time for me to understand this concept, but as with everything, practice should make it easy to understand it. We have after that enums and patterns, which I relate a lot to C++ variants, but better integrated in the language and as such far more usable.
The next few chapter go deeper in the object implementation of things with traits, operator overloading (which is done through traits) and huge list of useful traits. From there, we go to closures which are used in lots of places and are as versatile as in other languages that offer such concepts.
Before we go into containers, there is a description of iterators and I suppose the functional aspect of Rust. It’s not as well developed as the previous concepts, but just like for collections/containers, I did go through the chapter very quickly, and for someone who knows a little bit about these concepts, their Rust implementation is quite obvious to use properly.
Before tackling the crucial problem of concurrency in Rust (because of ownership and lifetime!), there are a couple of chapters now on strings and their variations, up to regex, and then input/output. They are pretty much standard, contrary to the concurrency chapter. There, we start with a simple fork/join pattern, but even with such a simple pattern, lifetimes can be messy! We get then additional tools with communication channels and finally the traditional mutexes. I was thinking maybe Rust didn’t have them, but even the references concepts are not enough to remove the usefulness of mutexes. The reason is actually simple: difficult to have a variable being read on one thread and written in another with just the normal ownership rules, as once the thread is started, you can’t have both reads and writes without mutexes.
We finish the book with macros that have actually accompanied us since the beginning (but I tend to avoid macros, so I have not really spent much time trying to figure Rust ones out) and then unsafe code. I liked the way unsafe code was tackled. The goal is obvious for the author: you can write unsafe code, but you should wrap it so that the unsafe code contract is enforced by your wrappers. I liked that approach a lot and it definitely makes lots of sense to keep Rust robustness.
Conclusion
For an introduction to Rust, this book does more than its job. It does assume that you know about algorithms and data structures before you dive in, but it’s fair to assume this considering the people targeted by Rust at the moment (people that know programming and that want to add a new skill).
Can’t wait to see the new edition.