laitimes

The future of Rust is here!

author:Advanced front-end advanced

大家好,很高兴又见面了,我是"高级前端‬进阶‬",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发!

The future of Rust is here!

高级前端‬进阶

1. The current state of Rust

In the Stack overflow 2020 survey, Rust was voted number one for the most popular programming languages, thanks to 86% of developers saying they would continue to use it. This is nothing new for language creators – Rust has been winning surveys since 2016. On the Tiobe index, Rust's popularity is also on the rise – ranking 18th among the most popular languages in September. It also did not fail in its own ranking of functional programming languages based on sentiment analysis, taking first place in the most positive sentiment category.

The future of Rust is here!

Rust was created in 2006 by former Mozilla developer Graydon Hoare, and as an alternative to the C++ language, Rust is slowly making a name for itself in the world of developers. It is now used to create network software, embedded computers, and distributed services or command lines. This proper proof of popularity has led global giant Microsoft to slowly move from the C++ language (due to increasing security issues) to Rust.

What makes Rust so popular among programmers when there are so many other languages to work with? One of the main reasons is that Rust solves many problems that are visible in different languages – so much so that some developers say that the creators of Rust have to take into account all the potential problems that are visible in C++. What exactly makes Rust so unique and worth learning Rust? Here are five reasons why we think Rust is the future.

2.Rust contrasts with other languages

Rust has advantages over many languages that developers know today, so let's compare them together.

2.1 Rust vs. C++ advantages

Rust will be safer compared to C++. Rust protects its own abstractions and those made by developers, while C++ lacks both options. Specific errors in C++ can lead to arbitrary behavior, and Rust helps you focus on what really matters.

C/C++ is still considered one of the most popular programming languages, but it often causes problems. Rust is easier to get started, the learning curve is not very steep, Rust does not have the technical burden like in C++, has easier concurrency, and the performance is comparable. Rust allows programmers to write unsafe code, but defaults to safe code (if you opt in, you can write unsafe code in Rust using the unsafe keyword — it comes with C++ by default).

2.2 Rust vs Java

2.3.1 Security

In recent years, security concerns against Java have continued to heat up, and security is the foundation of the platform's products. The linguistic nature of Rust gives it an inherent advantage to security. Of course, Java also has its solutions, such as the most vulnerable reflection problem, which can be circumvented by generating native code through GraalVM, but this solution is not yet mature

2.3.2 Performance

The Java ecosystem can write very high-performance code, but only if the NIO model is used. Because Java lacks Continuations/Fibers support, it is almost impossible to implement synchronous asynchronous programming with async/await, which modern languages support. This inevitably leads to a degradation in the organization and readability of the code. Project Loom hopes to solve this problem, but there is no specific time. In contrast, Rust has C++-like performance and is friendly to asynchronous programming

2.3.3 Costs

Java programs occupy high system resources, and even modules using JDK9 can only reduce the consumption of some memory resources. A typical microservice instance (based on Spring Boot) typically requires at least 200MB of volume and 500MB of memory. This is acceptable for SaaS deployments, but if you want to deploy locally, a product assumes more than 20 atomized microservices, no dependent third-party middleware, and at least 20G of memory is required for production deployments, which dissuades a large number of potential customers. As a system-level language, Rust has a small runtime overhead, and the resource occupation will be about 1/4 of Java under the same performance, and because Rust's Cargo package solves the "dependency hell" problem well, different atomized microservices can be aggregated into a service, which is very friendly to PoC

2.3.4 Protection of Property Rights

Java has many ways to decompile, and there are currently no great encryption/obfuscation tools, while Rust compiles directly into executable code. By encapsulating the core code in Rust, you can protect your code well.

2.3.5 Unification

Webassembly technology will be used for low-code builds, and Rust is the most mature development language for this technology. Rust also has obvious advantages in the design of the front-end and back-end unified architecture

When it comes to Java, it turns out that this programming language is much slower than Rust, especially considering that it is in sync with C in many scenarios. In addition, Rust has faster startup times and smaller memory footprints. Java uses a garbage collection mechanism for memory management, which can slow down performance (although it's worth noting that it makes programming easier).

2.3 Rust vs Python

2.3.1 Performance

One of the main reasons Rust surpasses Python is performance. Because Rust compiles directly into machine code, there are no virtual machines or interpreters between your code and your computer.

Another key advantage over Python is Rust's threading and memory management. While Rust doesn't have garbage collection like Python, the compiler in Rust enforces checks for invalid memory reference leaks and other dangerous or irregular behavior.

2.3.2 Memory Management

Python, like most modern programming languages, is designed to be memory-safe. However, Rust is out of reach for Python when it comes to memory safety, even without garbage collection.

Rust takes a unique approach to memory security, which involves an ownership system and a borrow checker. Rust's borrow checker ensures that references and pointers don't exceed the data they point to.

2.3.3 Error checking and diagnosis

Compared to most compiled languages, Rust is favored for its speed, guaranteed memory safety, super reliability, consistency, and user-friendliness. In programming, we have reached the point where speed starts to become effortless.

As technology evolves, it gets faster and faster, trying to do more in less time without as many trade-offs. Rust helps make this happen without getting in the way of developers' work. When technology tries to push the boundaries of what can be achieved, it also considers the security and reliability of the system, which is the main idea behind Rust.

2.3.4 Parallel operations

In addition to speed, Python has limitations in parallel computing. Python uses a Global Interpreter Lock (GIL), which encourages only one thread to execute concurrently to improve single-threaded performance. This process is a hindrance because it means you can't use multiple CPU cores for intensive calculations.

2.4 Rust vs Go

The main differences between Golang and rust are:

  • performance
  • Concurrent
  • Memory safety
  • Speed of development
  • Developer experience

2.4.1 Rust compiles slowly but executes fast?

Google introduced Go as an alternative to C++ that was simple to code and learn. Go provides and allows you to take advantage of Goroutines, through which people can run functions by simply including the Go syntax.

Despite these useful features and multi-core CPU support, Rust has the upper hand and surpasses Go.

Therefore, Go vs Rust: Performance is a feature where Rust gets more points than Golang. These programming languages were created to be equivalent to C++ and C. However, in the Rust vs Go comparison, Golang develops a bit faster than Rust.

While Rust's output shows an advantage over Go, Rust lags behind Go in compilation speed because Go beats Rust in this regard.

However, people don't care much about compile time, so overall Rust is the winner here.

2.4.2 Concurrency Design Rust is more concise?

Golang supports concurrency, which gives it an advantage over Rust in this factor. Go's concurrency model allows developers to install workloads on different CPU cores, making Go a coherent language.

So, in the case of you running a website that handles API requests, Golang goroutines will ask you to run each request as a child process. This process increases efficiency because it reduces tasks for all CPU cores.

Rust, on the other hand, has only one native await or sync syntax. Therefore, programmers prefer to use Go to deal with concurrency problems.

2.4.3 Both are memory safe, but Rust performance wins

Rust uses a compile-time strategy to achieve memory safety with zero-cost interrupts. If it is not a memory-safe program, Rust will not have the ability to clear the compilation phase. In fact, the good thing about Rust is that it provides memory safety.

The Rust compiler investigates every memory address you reference and every variable you use, and it ensures that programmers don't have buffer overruns.

In contrast, Go is fully automated at runtime. Therefore, developers will not have to worry about deleting memory when writing code. Therefore, both Golang and Rust prioritize memory safety features, but on the basis of performance, there is a possibility of data competition in Golang.

2.4.4 Is the learning curve in Rust much steeper than Go?

Since the main motivation for developing Go is simplicity and ease of use, most programmers consider it a "boring language" or "simple language". The limited features in Go make it very easy to learn and execute.

On the contrary, Rust comes with high memory safety features, which make the code more complex and the programmer less productive. The concept of ownership makes the Rust language an undesirable choice for many people. The learning curve for Rust is much steeper than for Go.

3. Why is Rust the future?

3.1 Rust improves memory safety.

One of the biggest problems plaguing developers is memory management issues, and an even bigger problem is that coding errors can lead to security vulnerabilities. In today's world, this is too dangerous. In large and popular websites or applications, data security breaches occur much more frequently than we would like, and this is usually an application vulnerability or misconfiguration.

Rust is considered memory safe. Rust code cannot have any null pointers, buffer overflows, or any other type of memory-related errors. It allows you to define how memory is managed and how values in memory are laid out, balancing control and security without sacrificing performance, which are important features of Rust.

Improving memory security is one of the main goals of Rust developers and the most important selling point of the language. Their code compiler is very strict, and every variable or memory address used is automatically checked. If any syntax errors, null values, null modifiers, or memory security issues are found, Rust will not compile the code and warn the unsafe code so that the problem can be quickly found and fixed. So how is it done?

In Rust, every value has an "ownership". When a value is passed or returned, ownership is given to a new range — and only one at a time. If the Owner of a value moves out of scope, the value is deleted. This way, Rust keeps track of memory and frees it automatically, preventing all errors from entering the main code. This means there won't be any surprises at runtime! The system analyzes memory management at compile time, helping to identify and fix errors faster and making garbage collection unnecessary.

But memory is much more than that. Building advanced systems is often associated with computationally intensive work and creating large amounts of temporary memory. This offload negatively affects the performance of any Java virtual machine, and many programming languages, including Scala, simply cannot handle it.

However, Rust can. It enables writing memory-saving code that runs with minimal or no runtime overhead without managing memory or using methods such as closures, is used for real-time or embedded projects, and integrates easily with other languages or projects.

3.2 The Rust community continues to grow.

The number of Rust communities and libraries is nowhere near as large as C++, but as the Rust language grows in popularity, so does the number of developers and enthusiastic people joining the Rust language. The number of frameworks, libraries, and development tools created by the community is already approaching 57k and is increasing every day.

In addition to a growing library of tools and frameworks, Rust has an active and enthusiastic community. Whether you need help solving a problem or looking for tips and tricks to use Rust, there are several places you can visit. In addition to community chats and user forums, Rust has an active subreddit section.

Take a look at some examples of emerging community projects:

  • redox, an operating system written in Rust;
  • CGMATH, a linear algebra and computer graphics library;
  • Iron, a concurrent networking framework;

3.3 Rust is fast and flexible.

Rust is considered a very efficient language to execute, and when using general-purpose code, it can run much faster than Scala, especially in performance-critical tasks. In some areas, it is likely to run up to three times faster than Scala or Java. At the same time, it's easy to introduce and use external packages, and Rust's cargo build tool makes coding much simpler.

There are a few factors that make Rust execute very quickly:

  • It is statically typed and compiled, allowing the compiler to optimize code for speed
  • It provides static assignment of features: similar to C++ templates, but more concise
  • It explicitly tracks ownership of variables. Rust doesn't assume that any variable with an address can be changed at any time, so it can be optimized without negatively impacting the code.
  • It expects variables to be immutable by default (so optimization is easier)
  • It provides algebraic data types and is also beneficial for optimization
  • It stores data without any overhead, no runtime or abrupt pause due to lack of garbage collection
  • It provides zero-cost abstraction

Rust can also be thought of as a programming language similar to Rust, but easier to optimize and with simpler high-level programming features.

3.4 Rust is widely available

If you have a project that needs to consider the importance of performance and low-level optimizations (as well as advanced Rust functional programming techniques), you also need a safe and stable execution environment, and Rust is recommended. Rust is considered a low-level language suitable for system-level development, as well as embedded and other performance-critical code. It is believed that it can also be used in 3D video games.

So why is Rust so loved by developers all over the world? First, it makes it very likely that the code you write will never go wrong because the compiler checks all the code. Problems detected in other programming languages at compile time do not appear in Rust. It is also good for writing secure time-saving abstractions, and its security virtually eliminates the need to use immutability.

Rust is also considered a language for emerging, innovative scenarios that care about the combined quality of performance, speed, and security.

Rust is a popular cryptocurrency and blockchain programming language, which is a great choice because it is easy to understand and learn, and it offers a unique combination of performance and security, which is exactly what developers are looking for. Rust also provides these developers with tools to start delivering their code faster. With Rust, cryptocurrencies will most likely not crash and are safer than using other programming languages. In the references, you can find a list of cryptocurrencies that use Rust.

3.5 Which big companies use Rust?

Although Rust is a relatively young language, it has been recognized by developers as a step in the right direction when it comes to improving the security and ease of programming. Not only are developers interested in Rust, but companies, especially major ones, have switched or plan to move to Rust. At the AllThingsOpen virtual conference in May, Ryan Levick, a Microsoft cloud developer advocate, explained that Rust as a language is "the industry's best opportunity to tackle multiple problems head-on." While Microsoft doesn't plan to stop writing code in C++ anytime soon, much of its infrastructure is slowly moving to Rust.

Microsoft isn't the only company that realizes the potential benefits of switching to Rust — companies using Rust include Dropbox, Sentry, Amazon, and Mozilla.

4. Conclusion

Looking at all the benefits Rust can offer, it's no surprise that the language is only growing in popularity. Rust solves some of the problems that cause many headaches in the developer community while maintaining the same level of performance as other more well-known programming languages. Since both developers and major players in the IT world recognize the potential of the Rust language, it's no exaggeration to say that Rust will one day become one of the most used programming languages in the modern world.

Resources

https://medium.com/scalac/5-reasons-why-rust-is-the-future-7ac731085339

https://www.reddit.com/r/rust/comments/a93dv8/cryptocurrencies_written_in_rust/

https://www.yisu.com/zixun/601244.html

https://zhuanlan.zhihu.com/p/539487030

https://medium.com/@knoldus/why-rust-for-embedded-development-9f2b827596f9

https://medium.com/@knoldus/why-rust-for-embedded-development-9f2b827596f9

https://medium.com/knoldus/why-rust-what-is-rust-837b32976a01