×
Community Blog Why Should You Learn Rust?

Why Should You Learn Rust?

This article aims to explain the importance of learning Rust.

By Leijuan

Preface

Rust has been rated the most wanted programming language on Stack Overflow for five consecutive years and has been well received.

1

The following part is my experience. At the same time, I will discuss the feasibility of Rust in combination with my daily work.

Rust and the Cloud

Microsoft has published an article called Microsoft: Rust Is the Industry's 'Best Chance' at Safe Systems Programming. It made one thing clear. The cloud is an industrial-grade product, and an industrial-grade safety system is needed. Safety here refers to Memory Safe, Type Safe, and Thread Safe in software development to ensure product stability.

We know after the release of new products, the tasks in the next period are mainly bug fixing. These bugs are usually not business bugs but code bugs caused by improper concurrency control, such as memory overflow, null pointers, and data errors. It is difficult for these bugs to be found by tests. If a good development language can help eliminate many potential problems in this area, is it very good?

Don't worry about performance because Rust is as fast as C and C++.

I agree with the viewpoint in this article that Rust is a very good infrastructure language for cloud product development. It guarantees stability. Everyone says the Internet focuses on cloud products. However, the key customers of cloud vendors are enterprise customers. Therefore, stability assurance comes first. Enterprises may use their businesses to try out cloud products.

Another piece of news comes from the article entitled Programming languages: Now Rust project looks for a way into the Linux kernel. In other words, the Linux kernel may embrace Rust, and Linus Torvalds has already recognized Rust.

2

Rust SDK Development

The advantages of using Rust to develop infrastructure cloud products will not be described here, but the performance is high, and the stability is good.

If we cannot develop products with Rust, does that mean ordinary programmers cannot use Rust? No. There is a product here.

Rust SDK development: Many cloud vendors have developed their middleware products based on Java and C++. Therefore, we need to develop SDKs that do not depend on programming languages, such as Java SDKs and Python SDKs.

Is there any difference in Rust SDK for products? I'll explain it to you here. Let's take Alibaba Dubbo as an example. If you do not know Dubbo, you can regard it as a distributed RPC call framework. Dubbo Rust SDK has the following features:

  • It provides support for Rust-based applications to access Dubbo services.

If the customer's system was originally developed using Rust, there should be nothing to refute this requirement for accessing Dubbo services through Rust, right? Rust Web framework Actix-web ranks high in performance tests. Dubbo Rust SDK, especially in I/O and network, is implemented through Tokio. So, asynchronization is supported by default, and performance is also very high.

Wait a minute. Is it so simple to write an SDK involving network calls?

The use of Tokio still involves a lot of work and is not so easy to write. In theory, this is the case. However, if you look at the tutorial of Tokio 0.2.22, you will find it very simple! This tutorial will not tell you how to write an Echo Server and Client but how to implement a Mini Redis. Complete documentation and code samples are available. You need to make some adjustments to preliminarily implement the functions of Rust SDK.

3

  • It provides support for Rust WebAssembly to access Dubbo.

We know Rust is the current preferred language of WebAssembly. Let's say you want to access Dubbo services in WebAssembly, such as in a browser. We only need to adapt to WebSocket and run the environment in WebAssembly. This way, we can access Dubbo services through WebSocket. The Rust SDK for Wasm virtually shares the Rust SDK code and makes some modifications.

WebAssembly also serves as the Universal Library, which can be understood if you have learned the wasmtime demo.

  • It provides support for Deno JavaScript / TypeScript to access Dubbo.

We do not want to argue which one is better between Node.js and Deno. Let's say some JavaScript / TypeScript development engineers have chosen Deno. We know Deno is built based on Rust. Deno Plugin is used to provide JS / TS calling interfaces, which is implemented through the Rust SDK of the corresponding product. You don't need to do too much work, and users of the Deno platform can access your released services through JS or TS.

There are many Node.js developers, but it is not easy to use C++ to develop a Node.js extension. A very small number of developers have this ability. Is it complicated to develop a Deno Rust plugin? No, it is very simple. You only need to refer to the Calcite framework, which has very good documents and samples. At the same time, you can combine it with the development kit on Crates.io. It is very simple to develop a high-quality Deno Rust plugin.

  • It provides support for the C language to access Dubbo services.

How is the C language supported to access Dubbo? Let's say some C programs of ARM devices want to access Dubbo services. We can export the cdylib library through Rust FFI and provide it for C language calls, eliminating the cost of developing the C SDK. This cost is not low. I also wanted to write a C SDK for RSocket previously, but I gave up after careful consideration. Now, I plan to provide an access interface for the C language through Rust SDK. If it works, it may be possible for other niche languages based on C to access Dubbo services, such as Lua.

  • It provides runtime support for other languages.

As mentioned above, Rust SDK provides a corresponding SDK for the C language through FFI. The same principle can be applied to other languages, such as Calling Rust from Python. There are many cases on the Internet. It is the same for Ruby. Is there any corresponding case for Java? Yes, the java-ext-wasm project. The WebAssembly Runtime environment is embedded into JVM to run WebAssembly. This WebAssembly Runtime is written in Rust.

Rust Developer Experience

The development experience of Rust is very good. However, JavaScript has the best developer experience. Let's compare the development experiences of Rust and JavaScript:

4

I think Rust is better than JavaScript in some aspects, such as built-in unit tests, examples, documents, code formatting, and Clippy (Linter). These features are better than JS.

It would be impossible to compare C++ and Rust in developer experience. Features, such as the consistency among Linux, Windows, and Mac platforms of Rust, are far better than C++. Rust has Crates.io, which is not available for C++.

Currently, one of the bad developer experiences in Rust is that the compilation speed is too slow. The compiler has done a lot of work, but there seems to be only Shared Compilation Cache (sccache) that can improve the compilation speed. I haven't used this yet. Perhaps, my project is not big enough. I haven't encountered a very slow compilation speed, and the speed is acceptable. The Rust community is also discussing pre-built dependency, which may take some time.

Rust Is Not as Complex as Expected with Low Learning Cost

At this point, some of you must want to try to learn and understand Rust. Is the learning cost high? I think it should be seen from several aspects.

  • Learning Materials

Among all the development languages, the documents of Rust are the best. The Rust website provides The Rust Programming Language, which can be called the best learning book. It is completely free and updated frequently. I think the quality is comparable to Programming Ruby of the Pragmatic Bookshelf. Rust is free and often updated. There are also other high-quality Rust-related documents, such as Rust By Example and Rust WebAssembly.

In addition, you will understand if you look at the Rust Cheat Sheet.

Other languages also have corresponding documents, but I would like to ask how many Java developers learn Java in Oracle Java? Rust does a different job. All the learning documents are first-class so you can learn without pressure.

  • Rust Module is not easy to understand.

I was really confused at first. I think Clear explanation of Rust's module system is very good. You will see after reading it!

5

  • Rust Ownership

Those learning about this recently may feel frustrated. At least, I am. This is also a feature of Rust. Without GC, it has to choose an unusual path to achieve the same effect as GC. However, it's not so frustrating. You can read some articles about Rust Ownership to understand more. In addition, I recommend this site. This man's Rust videos are very good and very easy to understand, and there are many materials. I believe you will understand it when you watch his videos and read his documents. You only need one good guide.

6

  • Rust Enum

Rust Enum is very powerful, which should be paid special attention to. I think Swift Enum can compete with Rust Enum. Results and Optional commonly seen in other languages are implemented through Enum in Rust.

  • Tokio

I won't talk about other features of Rust since other languages have them as well. Here is a description of Tokio.

Tokio is an asynchronous framework under Rust and is equivalent to Netty in Java. The well-known Deno framework is built based on V8 + Rust + Tokio. If you have JavaScript experience and know Promise, Future async / await of Tokio is similar to Promise async / await in JavaScript, and the Asynchronous stream of Tokio is similar to Asynclterablelterator. The cost of learning is not high. It may be a bit difficult for Java programmers to understand, but if you know the Reactive framework under Java, such as RxJava and Reactor, it is also easy to understand.

Summary

These are the conclusions from the individual learning of Deno and the writing of some Rust demos. This article explains the topic somewhat and may not be comprehensive. It's just for reference.

Finally, when learning a language, don't listen to any language master. I say read the documents yourself, write demos, view the code of some well-known open-source software, and draw your own conclusions.

Related Links

0 0 0
Share on

Alibaba Cloud Community

470 posts | 24 followers

You may also like

Comments

Alibaba Cloud Community

470 posts | 24 followers

Related Products