My Rust journey and how to learn Rust
Tags: rust, cloud, programming, devops
Published: 2023-01-17 13:08:16

> 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](https://survey.stackoverflow.co/2022/#most-loved-dreaded-and-wanted-language-want), 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
    
    ![2023-01-14 01-16-33屏幕截图.png](https://s2.loli.net/2023/01/14/mW5X6EgYLSfB3Qk.png)
    
* Most Loved vs. Dreaded
    
    ![2023-01-14 01-16-07屏幕截图.png](https://s2.loli.net/2023/01/14/69Wgk1zcHN8TQeb.png)
    

But Rust has a particular learning curve.

![pic from Rust User Team Samara - &Meetup1](https://camo.githubusercontent.com/1d24e64022fd725f1896890b3ce14c560f075dc1f80f0b0baae3ece8981c882a/68747470733a2f2f70617065722d6174746163686d656e74732e64726f70626f782e636f6d2f735f353445314239364546464546443239343536323930324443354239393731443335434436423635304243383744313230303341333041343635313737363230315f313538363531343237353631385f696d6167652e706e67)

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!)

<mark>Back in time to 5 years ago, I was leading the transformation of the company's infrastructure into a cloud-native stack.</mark>

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**](https://github.com/prometheus/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. <mark>I found that the operating system of this part of the machine is CentOS 5, and the kernel is 2.6.18.</mark>

I found that there are already similar issues in the community: [https://github.com/prometheus/node\_exporter/issues/691](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](https://web.archive.org/web/20170916192117/https://github.com/golang/go/wiki/MinimumRequirements), 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](https://dave.cheney.net/2013/06/18/how-to-install-go-1-1-on-centos-5), but at the same time, some known issues are mentioned in the article.

So I'm not going to keep fighting it.

**<mark>I want to re-implement one by myself</mark>**<mark>, which can also solve the above automatic crash problem.</mark>

**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.

<mark>Since I have a specific problem that needs to be solved, I need to implement a </mark> [<mark>node_exporter</mark>](https://github.com/prometheus/node_exporter/) <mark> to complete the transformation of the monitoring stack. So I learned Rust through the learning-by-doing mode.</mark>

I first took a quick look at the following:

* [The Rust Programming Language](https://doc.rust-lang.org/stable/book/): 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](https://doc.rust-lang.org/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](https://doc.rust-lang.org/std/index.html): 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](https://learn.microsoft.com/en-us/training/paths/rust-first-steps/)
    
* [rust-lang/rustlings: Small exercises to get you used to reading and writing Rust code!](https://github.com/rust-lang/rustlings)
    

videos:

* [Rust Crash Course | Rustlang - YouTube](https://www.youtube.com/watch?v=zF34dRivLOw&utm_source=blog.moelove.info&utm_medium=content)
    
* [Rust Tutorial - YouTube](https://www.youtube.com/watch?v=T_KrYLW4jw8&list=PLzMcBGfZo4-nyLTlSRBvo0zjSnCnqjHYQ&utm_source=blog.moelove.info&utm_medium=content)
    
* [Rust for Beginners - YouTube](https://www.youtube.com/playlist?list=PLlrxD0HtieHjbTjrchBwOVks_sr8EVW1x&utm_source=blog.moelove.info&utm_medium=content)
    

## 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!

{% embed https://blog.moelove.info/newsletter %}