source :https://tonybai.com/2024/04/22/gopher-rust-first-lesson-all-about-rust
To talk about which backend programming language has been the hottest in the past two years,If Rust claims to be second , and no one dares to say first .
Rust has topped the Stack Overflow’s most admired programming languages for 8 consecutive years, and it has even been referred to as the “perfect programming language” by Jack Dorsey, the co-founder of Twitter:
rust is a perfect programming language
— jack (@jack) December 24, 2021
1. Why should I learn Rust?
Learning Rust isn’t about riding the hype train; it’s about practical development needs. In certain situations, Go’s performance isn’t particularly good due to issues like STW. From benchmarks, Rust’s performance is three times better than Go’s: The Computer Language Benchmarks Game Visualization In our own business, after rewriting a gateway service in Rust, performance improved by about 70%. This is a significant improvement for our business scenario. Moreover, Rust’s widespread use in the Linux kernel validates its reliability, making Rust worth trying.
However, the rise of Rust in certain domains has indeed sparked some dissatisfaction and controversy in other programming language communities. Particularly, the proposition from some in the Rust community to “Rewrite Everything in Rust” has made many programming language communities, especially the C++ community, quite uneasy. The Go community, on the other hand, is relatively more open and friendly. The mainstream view is that Go and Rust can complement each other, with each language playing its role in its respective areas of strength. Through cooperation rather than confrontation, developers can be provided with better choices. For more details, you can refer to an article co-authored by Steve Francia, former product manager of the Go team and author of Hugo, Rust vs. Go: Why They’re Better Together.
In other words, Go is still my primary language, but considering the requirements of my work, I need to systematically learn Rust. To avoid the “from learning to giving up” scenario, I plan to learn Rust while outputting. On one hand, this can motivate me to learn, and on the other hand, I hope to interact with readers promptly and correct any misunderstandings in learning.
I’ve always believed that when you start learning a new language, you must understand its history and current status. This way, you can build an overall understanding of the language and anticipate its future direction. Moreover, it can establish a sense of “security” in learning, believing that it can bring you enough value and returns, thus enabling you to learn more confidently.
In this article, I’ll first explore the history of Rust’s development and its current state, as well as its unique design philosophy. I’ll also make a simple comparison with Go, hoping to provide myself and the readers with a preliminary understanding of Rust.
2. Rust’s History and Current Status
2.1 The Birth and Evolution of Rust
Rust was born in 2006, a year earlier than the “conspiracy” of the three Google giants to create the Go language. However, compared to the three founders of Go: Ken Thompson, Turing Award winner, co-inventor of C syntax, and father of Unix; Rob Pike, leader of the Plan 9 operating system and original designer of UTF-8 encoding; and Robert Griesemer, one of the designers of the HotSpot virtual machine for Java and the JavaScript V8 engine for the Chrome browser, the identity and status of Rust’s father, Graydon Hoare, were not so “prominent”. At that time, he was just a Canadian developer under 30 years old working at Mozilla Research:
The birth of a new generation programming language often comes with a story, such as the founders of Go frequently encountering long compile times in C++ projects at Google. Whenever they started compiling a C++ project, they had to wait for a long time, during which they could drink several cups of coffee. This deeply impressed them and made them realize the need to design a new language with faster compilation speed, thus Go was born. Similar to the “drink coffee, wait for C++ project to compile” scenario, the birth of Rust also has a story:
In 2006, 29-year-old Hoare returned home to Vancouver one day, only to find that the elevator was broken, and the elevator software crashed! He had to climb the stairs back to his 21st-floor apartment. As he climbed the stairs, he felt very annoyed. He thought, “We programmers actually can’t create an elevator that can work properly without crashing!” Hoare knew that many such crashes were caused by issues with memory usage in programs. The software inside devices like elevators is usually written in C++ or C, languages known for allowing programmers to write code that runs very fast and is quite compact. The problem is that these languages also make it easy to inadvertently introduce memory errors, which can lead to crashes. Hoare decided to do something about it. So he opened his laptop and started designing a new programming language, one that he hoped could write small and fast code without memory errors, and he named it Rust.
This story is obviously unverifiable. But what can be confirmed is that for several years starting from 2006, the personal language project Rust created by Hoare was not actually used to improve elevator systems, but was sponsored by Mozilla and used in the continuous development of Mozilla’s browser engine Servo. Mozilla officially announced the project in 2010, and Hoare also introduced the Rust language for the first time in a speech in 2010:
The first line of Rust code was also open-sourced in 2010:
In addition, the initial Rust compiler was implemented in OCaml, and in 2011, the Rust team re-implemented the compiler in Rust based on LLVM and achieved bootstrapping. That same year, Rust also got its own logo, inspired by a bicycle gear:
In 2012, Graydon Hoare was interviewed by InfoQ and talked about leading the Rust team in developing Rust, a systems programming language at Mozilla, including Rust’s features, advantages and differences compared to C/C++/Java/Go, and Rust 1.0 release plans.
However, in the following year, in 2013, Graydon Hoare resigned as leader of the Rust team due to exhaustion, leaving his own Rust team and distancing himself from Rust development. Hoare’s departure was a significant loss for the Rust team and the language itself, but the Rust community and team took proactive measures to ensure the continued development and evolution of Rust.
In November 2014, Rust officially announced cargo and crates.io. Cargo is Rust’s project build manager, while crates.io is the central package repository for Rust code maintained by the Rust team. With cargo, developers can easily build and publish packages to crates.io or pull dependencies of Rust code from crates.io.
On May 15, 2015, Rust reached a milestone moment: Rust 1.0 was officially released!, which was 3 years later than the release of Go 1.0. However, as the official blog stated, “the release of version 1.0 marks the end of the chaos. This version is the formal beginning of our stability commitment, providing a solid foundation for building applications and libraries. From now on, major changes are essentially out of scope (some minor warnings apply, such as compiler errors).”
After the release of Rust 1.0, the release cycle and rhythm of Rust were determined, with a stable version released every 6 weeks. Following this rhythm, Rust 1.1 Beta was released simultaneously with Rust 1.0. After six weeks of testing, Rust 1.1 Beta became Rust 1.1 stable, and Rust 1.2 Beta was released, and so on. Of course, Rust also has a nightly build version, which contains the latest but unstable features. Compared to the Go community and developers who can only “get high” twice a year, Rust developers and the community are more fortunate to “get high” every six weeks!
Rust’s evolution is driven by RFCs (Request For Comments), and this measure was established based on RFCs before the release of Rust 1.0. This is similar to the Go Proposal process, but it feels more standardized and rigorous, which of course is related to the composition and rules of governance structures of the two languages.
However, the
evolution and development of Rust is not as smooth and perfect as one might expect. For example, at the Rust 2018 edition launch event, Alex Crichton, one of the core members of the Rust team, mentioned in the keynote speech that in the evolution of the Rust language, some past design decisions may not have been optimal, which sometimes hinders the language’s progress. One example is the error handling mechanism. However, Rust’s development team has proven that they are good at introspection and adjustment. For example, the aforementioned error handling mechanism was greatly improved with the introduction of the “Try” trait in Rust 1.26, and with the release of Rust 1.31, the Rust 2018 edition resolved many issues with Rust’s design.
2.2 Rust’s Current Status
In the past decade, Rust has gradually developed from a personal project of Graydon Hoare to a popular systems programming language with a large and active community. According to GitHub’s 2020 Octoverse report, Rust was the fastest-growing programming language in the GitHub open-source community, with 235% more contributors than in 2019, making it the most loved language in the Stack Overflow Developer Survey for five consecutive years from 2016 to 2020.
However, it should be pointed out that Rust is still not widely used in large-scale commercial projects, especially compared to C/C++/Java/Go. The main reason is that Rust’s ecosystem and maturity are still not comparable to those languages. For example, the Alibaba e-commerce team mentioned in 2017 that they have 100,000+ Java developers and have invested heavily in Java and the JVM ecosystem. They also mentioned that in recent years, Alibaba has considered using Rust to solve some distributed computing problems, but has not yet considered using Rust in large-scale commercial projects due to concerns about the immaturity of Rust’s ecosystem.
However, Rust is gradually being adopted and applied in some fields and areas where C/C++ is traditionally dominant, such as systems programming, game development, and blockchain systems. For example, in the blockchain field, in addition to the Solana project I mentioned earlier, the Parity project, which is known for the development of the Polkadot blockchain framework, is also developed in Rust. In addition, in the blockchain field, some small and medium-sized blockchain projects also use Rust to develop blockchain systems. For example, the Bitcoin Light client library BTC-Rust implemented by the Bitcoin-NG team is developed in Rust.
3. The Design Philosophy of Rust
The design philosophy of a programming language is like the values of a person—it dictates its behavior. If you don’t agree with someone’s values, it’s hard to maintain a continuous relationship, as they say, “different paths do not converge.” Similarly, if you don’t agree with the design philosophy of a programming language, you’ll likely encounter the problems mentioned earlier in your subsequent language learning, which may dampen your motivation to continue learning. Therefore, before diving into Rust syntax and coding, let’s first understand Rust’s design philosophy. After understanding these, you’ll have a deeper understanding of why you’re learning Rust.
3.1 Core Values of Rust
In June 2019, Rust core team member Stephen Klabnik delivered a speech titled How Rust Views Tradeoffs at QCon London, where he outlined his personal understanding of Rust’s core values, which are the points Rust’s team refuses to compromise on when making design decisions, including memory safety, execution speed, and productivity:
According to Stephen Klabnik, these three core values are ordered, with memory safety being paramount, followed by high performance, and finally productivity. When conflicts arise among them, decisions are made according to the highest value!
This is consistent with the official description of Rust:
The “Reliable” corresponds to memory safety, while “efficient” has two meanings: runtime efficiency and high productivity during development.
These three values are the design goals of the Rust language and the essence of its characteristics and advantages. After losing Graydon Hoare, the father of the language, these values became the fundamental basis for the Rust core team to determine the direction of language evolution.
- Memory Safety
Memory safety is the most important value for Rust. It means that Rust programs won’t suffer from memory leaks, buffer overflows, dangling pointers, and other memory-related errors at runtime (without using unsafe code). These errors not only lead to program crashes but can also result in security vulnerabilities. Rust ensures memory safety by features like ownership, lifetimes, and borrowing, which are thoroughly checked at compile time.
Rust’s memory safety mechanism not only enhances program stability and reliability but also reduces development and maintenance complexity. With Rust’s ability to detect memory errors at compile time, developers don’t have to spend a lot of time and effort searching for and fixing these errors.
- High Performance
High performance is the second core value of Rust, closely following memory safety. One of Rust’s design goals is to be a high-performance systems programming language. Through features like zero-cost abstractions, move semantics, and generic programming, Rust enables programs to achieve performance comparable to traditional systems programming languages like C and C++ at runtime.
Rust’s high-performance mechanism not only improves program execution speed but also reduces hardware costs. Because Rust can better utilize hardware resources, Rust programs typically outperform programs in other languages with the same hardware conditions and resource overhead.
- Productivity
Productivity is the third core value of Rust. One of Rust’s design goals is to be a language that enhances developer productivity. With features like the Cargo package manager, smart editor support, rich library ecosystem, and detailed system documentation, Rust makes it easier for developers to write, debug, and maintain Rust programs.
3.2 Secondary Values of Rust
Stephen Klabnik also summarized three secondary values of Rust:
We see that Rust’s secondary values include ergonomics, compile times, and correctness. These values are also design goals of the Rust language, but unlike the primary core values mentioned above, they are subject to compromise.
Ergonomics refers to the ease of use of the Rust language, which is an important design goal of Rust. Rust aims to make it easier for developers to write Rust programs through simple syntax and a rich library ecosystem.
Compile Times refer to the compilation time of the Rust compiler. Rust’s compiler is slow, which is a problem that the Rust team is actively working to optimize. However, the Rust team is more concerned about the final execution speed of the binary than making the compiler faster, hence why compile time is a secondary value.
Correctness refers to the correctness of Rust programs. Rust really cares about whether your program is correct and aims to ensure the correctness of Rust programs as much as possible through a powerful type system and static checks. However, Rust is not willing to rely entirely on types and proof assistants to prove the correctness of your code.
3.3 Comparison with Go’s Values
Let’s compare Go’s official introduction to Go’s implicit values (design philosophy):
In the official description of Go, there are three keywords: Simple, Secure, and Scalable.
Simple is the primary design principle of Go. The designers of Go hope that Go can be simple and easy to use, enabling developers to learn and use Go more quickly to rapidly develop production capabilities. Go has a simple and understandable syntax, and it removes many complex features found in other programming languages, such as type hierarchies and inheritance, making Go more concise, easy to learn, read, use, and maintain.
Secure is about making Go more secure and reliable, avoiding common security vulnerabilities found in many other programming languages. Go achieves this by automatically managing memory through garbage collection, avoiding common memory leaks and buffer overflow issues found in many other programming languages. Additionally, Go provides lightweight goroutines and channels, making it easier for developers to implement concurrent programming. With data race detection tools, Go also prevents common data race issues in concurrent programming. Furthermore, Go provides a simple and easy-to-use explicit error handling mechanism, ensuring no error handling is missed by developers.
Scalable is reflected in Go’s engineering orientation, built-in concurrency, and a philosophy that emphasizes composition. The designers of Go hope that Go can better support scalability, enabling Go programs to better adapt to different organizational scales, workloads, and hardware environments. Go achieves this through simple syntax, module-based reproducible build management, extremely fast compilation speed, high-quality standard library, practical toolchain, powerful built-in concurrency mechanism, and interface-oriented programming, making Go programs more scalable and productive.
4 Conclusion
In summary, Rust prioritizes safety, low-level control, and optimal performance, while Go emphasizes simplicity, security, scalability, and engineering efficiency. There are differences in their positioning and design philosophies, but they also share some common characteristics, such as modern toolchains and active communities.
In this article, we’ve learned about the birth, current development, and unique design philosophy of Rust. By comparing it with Go, we can see some differences in their backgrounds, goals, and design philosophies.
As software systems become increasingly complex, the demand for security, performance, and concurrency is also rising. As a new language focused on low-level systems programming and performance optimization, Rust is attracting more and more developers’ attention. I believe that through comprehensive and systematic learning of Rust later on, we will all gain a deeper understanding and mastery of Rust.
If you find Rust’s values align with yours and you agree with Rust’s future development, stay tuned for the next article, where we’ll start hands-on learning Rust!