My Rust journey and how to learn Rust
Learn Rust through the learn-by-doing model
I'll share my Rust journey, how I learned Rust and some free Rust learning resources.
Rust has become more and more popular. Through the StackOverflow 2022 Developer Survey, we can see that many people are interested in Rust.
Rust is on its seventh year as the most loved language with 87% of developers saying they want to continue using it.
Rust also ties with Python as the most wanted technology with TypeScript running a close second
Most Wanted
Most Loved vs. Dreaded
But Rust has a particular learning curve.
This made me want to share my Rust journey, why I chose Rust, and how to learn Rust.
Getting connected with Rust
I had heard about Rust when it was first released, and my impression was that it was a system programming language that could replace C/C++ and was safe enough. But I didn't learn and use it. (I've only used it to write Hello World!)
Back in time to 5 years ago, I was leading the transformation of the company's infrastructure into a cloud-native stack.
I need to construct a monitoring stack based entirely on Prometheus to replace a set of monitoring software in the company with more than 10 years of history. And some other monitoring software, such as Nagios, Zabbix, and Graphite.
Yes, you read that right, we are using a lot of surveillance software. There are a few reasons for this:
A single software cannot meet all needs
The team is scattered, and most of the time, new software is introduced just to meet specific needs, rather than to solve the problem
Anyway, here are some historical reasons.
And, from what I mentioned above, we have a set of self-developed monitoring software with a history of more than 10 years, as you can see, our infrastructure is slow to iterate.
And because we have our physical data center, this also leads to many old machines in our servers that have not been updated. (This is one of the reasons why I used Rust later)
I first replaced the monitoring stack in a newly launched small data center, with about 400 machines, and the effect was good. Using Prometheus to complete the monitoring of all the servers in this small data center and the various services running on them. There are also Dashboards created for them in Grafana, and alarm notifications created through Alertmanager.
Later, I promoted these transformations in two data centers, and overall it was relatively smooth, including the monitoring of Kubernetes was also completed during this process.
But when it was implemented in the last data center, I faced the biggest challenge.
node_exporter failed to start on some machines, and some machines crashed automatically after running for some time.
I started to investigate this issue. For the automatic crash issue, I temporarily fixed it by adding a restart script.
I'm mainly concerned with why node_exporter won't start. I found that the operating system of this part of the machine is CentOS 5, and the kernel is 2.6.18.
I found that there are already similar issues in the community: https://github.com/prometheus/node_exporter/issues/691
At the same time, I also noticed that the Go documentation clearly stated that CentOS 5 is not supported, and a kernel of at least version 2.6.32 or above is required.
(I forgot the minimum dependencies when I checked, but through the web archive, I see that the minimum kernel version required in 2017 is 2.6.23)
After some searching, I also saw something like How to install Go 1.1 on CentOS 5.9, but at the same time, some known issues are mentioned in the article.
So I'm not going to keep fighting it.
I want to re-implement one by myself, which can also solve the above automatic crash problem.
In the end, I used Rust to implement a tool similar to node_exporter and completed the upgrade and transformation of the monitoring system.
This is where my journey started with Rust in production.
Next, let me introduce why I chose Rust.
Why choose Rust
I have introduced some background above. At that time, the easiest choice should be Python, which is simple enough and rich in ecology. At the same time, I also have many years of experience in Python development, I can quickly build the tools I need.
The reasons for not choosing Python are:
Not all of these machines have a Python environment, and the versions of Python are also different. I was asked not to modify the environment on these machines as much as possible;
Since I may make some modifications later, I think the subsequent distribution may not be convenient;
Then I rethought my goal:
- Can be compiled into binary executable files for easy distribution and deployment. I used Ansible for unified deployment.
So a more suitable option is C/C++/Rust.
I have more experience in C development and a little experience in C++. For my first requirement, the above three languages can be easily met.
When most people compare Rust and C/C++, they are comparing their performance and safety.
And in my use case at the time, I don't think the results in the other two languages would be worse than in Rust, although these are also considerations. And since I was just starting to learn Rust at the time, it might be worse than my C implementation.
But I want more challenges, try something new, and in terms of Prometheus monitoring, the C/C++-related ecology is not very active. Another point I think Rust will have great development in the future.
So in the end I chose Rust.
How I learned Rust
Rust is not simple, and it's not quite the same as other languages, so some practices that work in other languages may not work in Rust.
Since I have a specific problem that needs to be solved, I need to implement a node_exporter to complete the transformation of the monitoring stack. So I learned Rust through the learning-by-doing mode.
I first took a quick look at the following:
The Rust Programming Language: This book is very complete, I didn't read it completely at first. Instead, use it to understand the main concepts and some usages in Rust.
Rust By Example: There are many examples here, and you can also increase your familiarity with Rust by practicing these examples;
Rust std lib docs: Documentation of the standard library, a quick overview, understanding some keywords, modules, etc. But it is not necessary to read it in its entirety initially.
This way I quickly implemented a basic node_exporter version. Then continue to iterate and apply it to the production environment, and completed the construction of the Prometheus monitoring stack.
Later, I continued to implement some small tools in Rust, learned its best practices, and learned some open-source projects implemented in Rust to increase my Rust experience.
Recommend some Rust learning resources
There are many learning resources for Rust now. In addition to the ones I listed above, I recommend the following free content:
Take your first steps with Rust - Training | Microsoft Learn
rust-lang/rustlings: Small exercises to get you used to reading and writing Rust code!
videos:
Summarize
This is how my Rust journey started, and it continues.
Although I focus on Cloud Native and Kubernetes-related technologies, and now I write more Go language, I also still write some tools in Rust and use Rust in WebAssembly.
In the future, I will also share relevant content. If you are interested in my articles, welcome to subscribe to my Newsletter!