Rust: fear => love => hate => love => disillusion
My journey through Rust was very tortuous and tormented. During this article, I want to retrace my steps towards the discovery of that amazing programming language.
First encounter: fear
When I was at the university, a colleague of mine and I have been started working on a project for the course of Data Compression. That was something very low-level and bitwise. We already knew a bit of C, Java, Python, and JavaScript. Every of those, except C, were not suitable at all to work with bits, and since we don’t want to work with C anymore, we decided to pick a brand new programming language (the worst idea ever when you have to finish your project as soon as possible).
We then started looking for a programming language with the following characteristic:
- Fast
- Compiled
- Easy to learn
We already heard about both Go and Rust and we were excited to learn Rust because the Stackoverflow Survey said that Rust was the most loved programming language for developers and, of course, we were developers and we wanted to love it too.
The first impression was: FEAR!
WTF were those ::
, i32
, println!("{}", something)
? WTF is this “Ownership”?
After this first impression, we quickly decided to go for something more similar to the stuff that we already knew and there comes Go. It was love at first sight as you may deduce reading my other stories on Medium. We developed the whole project in Go and everything went well.
Happy ending.
Second (almost) encounter: love
University was over and I started working for a software company in Rome. My first task was to fix a bug on a system written in Go. No problem!
When I had to pick a language for a brand new system for manipulating images I was determined to pick Rust. I started reading the Rust book and I found different aspects of the language that would have been killer features for my application:
- memory safety: if there is a memory problem, your code does not compile (almost) always;
- performance: Rust is very fast!
- fearless concurrency: manipulating images is something like manipulating matrices. Parallelism is the key to scaling up with no fear. Rust has great support for concurrency;
- that taste of functional: I love functional programming and I wanted to love Rust too.
That was the first time that I fell in love with Rust ⚙️.
However, given that the company already had software written in Go, I eventually had to pick Go. I started feeling frustrated sometimes because of the lack of control over some features (thread locality etc), but, in the end, everything has gone ok.
Happy ending 2.
Third encounter: hate
After changing different jobs, I started working for another company and they use mainly Elixir 💧, Elm 🌳, and, of course, Rust ⚙️.
I was very happy to join that company and still, I am! Very excited to work on Rust, finally. I can love Rust as 86.98% of the developers in 2021 does.
I started doing something using Elixir (one of my favorite programming languages) and I finally had to work on a CRUD microservice using Rust and it was… frustrating.
I hated Rust. It sucks!
I want to be focused on the domain of my application, reducing time-to-market, releasing it as soon as possible and I want to write as many tests I can but it is not quite straightforward in Rust.
I spent so much time finding out why I cannot return that variable, writing stupid impl
s that could be implicit and hating Rust.
I had to duplicate pieces of code because the returned value of that function that could have been reused is something opaque and internal to some library which is changed almost every week.
Sad ending for now.
Forth encounter: love
I come to an end that, at the moment, Rust is not the best choice for a CRUD microservice. It is too young. Its libraries are still limited and it lacks a consolidated framework for the web (that I like).
But I cannot rest and my struggle was to understand why so many developers love it. So I decided to solve Advent of Code 2021 puzzles using Rust.
Ok. Now I understand why they love it. I love it too.
It is fun and pleasant to write Rust code. It is fun to solve algorithms using it and I like to run benchmarks to see it performing very fast.
My doubts about the hype of love for Rust are finally resolved.
What I like about Rust
There are some features that I really like aboutRust from the developer's point of view (that’s why I jump over performance, single binary, and so on). Here are some:
- The ecosystem: the standard library is very complete. It has great support for different types and data structures that help the developer write clear code;
Result<T,E>
andOption<T>
: yeah! They rock!Result<T,E>
is very useful to handle errors andOption<T>
to handle null values. They are inspired by Haskell’s type classes and it should be enough to appreciate it. Using type classes makes your code clear and safe. This was a clever move. Kudos Rust!- Ownership: at the first glance it may look scary but I can assure you that after spending different hours looking (and yelling) at the output of the compiler, trying to understand what is wrong in your code, you’ll appreciate it;
- Compiler errors: errors printed by the compiler are very clear and concise. It will bring you to the solution, explaining why you did something wrong;
- The debugger: LLDB rocks and it is well integrated with VSC;
- Macros: Rust’s macros are very powerful. They are the icing on the cake of Rust.
What I dislike about Rust
Here are some features that I don’t like about Rust from the developer’s point of view:
- Steep learning curve: come on guys! It is clear that learning Rust is not easy at all. You fail so many times before being confident in writing Rust code;
- Primitive types: I don’t like the fact that primitive numbers have explicit size annotation. I’m sure there are many reasons behind this decision but I don’t like that;
- Unsafe: unsafe is treated as a magic power of the language. If you use it, you feel a lot of responsibilities;
mod.rs
: I don’t like the need for explicit export modules throughmod.rs
. When you change something (i.e. the name of the module), you have to change it in different points;- Lack of reflection: I know that reflection implies the knowledge of type details at runtime, but it feels like a missing feature and sometimes you end up copy-pasting code in a huge
match
.
I would pick it for…
However, at the time of writing, I am reading tons of books and articles about programming in Rust for microservices, system programming and videogames and I think that Rust will be best suitable for the following use-cases:
- CPU bound microservice: when you have to deal with something that requires a lot of computation, Rust may be the answer;
- Command-line tools: Rust is taking steps in the Linux kernel and its adoption for developing command-line tools is straightforward;
- Ass-kicking solvers: it is both fast and safe and provides a good level of abstraction, so it would be a good choice for developing solvers.
Conclusions
I don’t know what will be next, but I am enjoying programming in Rust and I hope you will too.