I love Rust language!

After a few months of learning Go and Rust seems I’ve come to my conclusion: I love Rust. Go isn’t bad, and I’m using it at my work in Google; in fact I would consider Go for lots of things as well. But Rust is on a whole different level, it’s making me happy and amused to learn it; I’ve recovered the joy from programming that I had when I learnt Python.

What makes Rust special?

There are lots of things to gets me inspired with Rust, so I don’t know where to start from. I guess that this depends on your interests. For me as Python is the king of the interpreted languages, Rust is the king of the compiled ones.

Rust was created by a Mozilla employee and later adopted in Firefox Quantum for a CSS renderer that replaced the previous C++ engine, this has made Firefox faster and safer. Since then, I jumped back from Chrome to Firefox.

C++ is one of the fastest languages out there. It’s hard to tell if C is faster than C++ or not, it depends a lot on what you want to do and how you do it. Rust was created as a replacement for C++ that should be equally fast, or even faster if possible. So nowadays Rust joins the club of top performing languages. Every other language is going to be slower than these three by at least 2x or 4x, and that’s the best case scenario. For example, Haskell can compile and get in some scenarios 50% of C++ speed. Java, around 25%. But these numbers are in reality way worse, because as you grow your programs and stop heavily optimizing and adding complex parts, they get even slower.

This is not the case for Rust. In fact, it might have at some point an advantage to C++. All three languages (C, C++ and Rust) have this mantra of “zero cost abstractions”, which means that you can build more and more complex systems and get almost no penalty from doing so. So they’re ideal for creating operating systems, browsers, game engines and so on.

The main difference between C++ and Rust is that Rust is memory safe. You have to purposely screw up with the memory by using “unsafe {}” blocks of code, and you can do 99% of your work without even thinking of using that. Well, probably 100%, as long you’re not trying to build the next-generation fastest library ever, Rust has lots of ways of implementing really fast data types.

Rust memory allocation is like C++ but uses a “borrow checker” that ensures the memory is freed properly in a guaranteed way. And trust me, I tried to screw with it and I couldn’t manage to confuse enough the compiler to let me build something that would at any point, leak or double free memory. In contrast with Go or Java, where they use a garbage collector, Rust is not garbage collected. Instead it’s “statically collected” if you like the term. This means that Rust programs are the fastest possible without having to worry about pointers and memory. For example, Go suffers from micro-freezes where the garbage collector has to run and every possible code will halt at that point for a few milliseconds.

Another cool thing in Rust is the “zero cost abstractions” thingy. There are so many ways of building crazy abstractions that in fact, they almost have no impact on the execution time. Generics and Traits for example. So you can build libraries that automate a lot of stuff for you in Rust without being any slower than doing the same thing manually every time (well, some penalty may appear in some cases, but it’s tiny).

Rust also has a lot of inspiration from functional languages. Rust isn’t functional, but like Python, it allows you a lot of constructs that resemble to functional languages and they work really well. The compiler doesn’t care about pure or non-pure functions, but they also borrowed this “intelligence” from their compilers as for guessing data types. It’s amazing that types can be inferred not just from the current and past lines, but also from future code. This greatly reduces the amount of code to be written, as you only have to define types in a few critical places. The remaining code is clutter-free most of the time.

You should learn Rust!

I will admit it. Rust is not an easy language to start on. The learning curve is a bit steep and either you’ll have to learn a lot of new mechanics on memory ownership (if you don’t know C or C++) or you’ll have to unlearn a lot of bad practice in pointers (if you knew C or C++ before). Rust makes things in a different way and it’s a bit of fresh air into programming since the last 20 years.

I really believe that Rust got the real way of programming ™ and eventually other languages will follow. So face it now or face it later, the concepts of ownership and borrowing will get onto your favorite programming language at some point, or your language will remain garbage collected and slow forever. Yes, Java, I’m looking at you.

Even if you will not use Rust at work or in production, it will teach you how to be a better programmer. Because one of the best things in Rust is that the compiler does not blame you, it teaches you. You can translate that knowledge to your regular programming in another language, and you’ll get safer, better code.

Rust should be a good fit for REST API services that have lots of logic and/or manage lots of data. In my tests, Rust ORM (Diesel) has shown to be 20x faster than Go ORM. On top of that add abstractions and logic on your own and the difference could be even bigger. Millions of users and huge data? No problem!

It also has good support for all platforms. Developing from Windows, Linux or Mac OSX shouldn’t be a problem at all. It also works on embedded or other exotic platforms. They’re working hard to increase the amount of target platforms to try to cover everything that C covers, but for now they have already a lot.

In short, Rust is an all round language. Multipurpose, multiplatform, fast, safe,… what else do you want? Give it a try!