1.0 Introduction
1.0.1 Aside
This project, including both the code and the notes, was recorded while I was self-studying Rust. There may be places where the writing is not precise enough or the expression is not clear enough, so I ask for your understanding. If you can benefit from it, that would be even better. Here I recommend the original video Rust Programming Language Beginner's Tutorial (Rust Language / Companion to The Rust Programming Language) [completed].
1.0.2 Why Use Rust
Rust code is reliable and efficient.
-
Rust can replace C and C++. With similar performance, Rust is safer than them. In practice, the most obvious difference is that Rust does not require you to compile every few lines just to check for errors, the way the first two languages often do. Specifically:
- Memory safety: no null pointer dereferences, dangling pointers, or data races
- Thread safety: multithreaded code can be guaranteed safe before the program runs
- Avoids undefined behavior: such as out-of-bounds array access, uninitialized variables, and using freed memory
Rust provides modern language features such as generics, traits, and pattern matching.
Rust provides a more modern toolchain. Rust's Cargo and Python package managers such as pip follow the same philosophy. Anyone who has used C/C++ knows that dependency configuration for those languages can be cumbersome, while Python's package management tools are flexible and simple. Cargo gives Rust users a similarly comfortable dependency-management experience while still delivering C/C++-level performance.
1.0.3 Suitable Scenarios
When you need speed: Rust can control memory as finely as C through
unsafe, while also providing the conveniences of modern high-level languages, such as the ownership system and pattern matching. Python is a very high-level language with high development efficiency, but it sacrifices performance and control.When you need memory safety: Rust provides strong memory-safety guarantees through compile-time static checks, making it extremely suitable for scenarios where memory errors must be avoided, such as operating systems, embedded development, and network servers.
When you need efficient use of multiple processors: Rust provides native support for efficient concurrency and multi-processor programming without sacrificing safety. This is especially important for scenarios that handle high throughput and concurrent tasks, such as web servers, distributed systems, and real-time computing.
Areas where Rust excels:
- Web services
- WebAssembly (C# and Java lag far behind Rust and C/C++ in performance comparisons)
- Command-line tools
- Network programming
- Embedded devices
- System programming
1.0.4 Comparison with Other Languages
| Category | Language | Features |
|---|---|---|
| Machine language | Binary instructions | Closest to hardware, executed directly by the CPU |
| Assembly language | Assembly | Uses mnemonics instead of machine instructions, such as MOV AX, BX
|
| Low-level languages | C, C++ | Closer to hardware, provide limited abstraction |
| Mid-level languages | Rust, Go | Performance close to low-level languages, but with higher abstraction |
| High-level languages | Python, Java | Higher-level abstraction, easier to read and use |
High-level languages and low-level languages are not absolute opposites; they form a continuous spectrum:
- Lower-level languages provide more control over hardware, but code is more complex to write and development efficiency is lower.
- Higher-level languages provide more abstraction and automation, but they may introduce runtime overhead and reduce fine-grained hardware control.
Rust's advantages:
- Good performance
- Strong safety guarantees
- Excellent concurrency support
As a mid-level language, Rust has these advantages over other languages:
- C / C++ offer excellent performance, but they are not safe enough; Rust can maintain roughly the same performance while also ensuring safety.
- Java / C# can guarantee memory safety with a GC (garbage collector) and provide many features, but their performance is not as good; Rust not only offers comparable safety, but also stronger performance.
1.0.5 Rust's History
Rust began as a research project at Mozilla, and the Firefox browser is an important real-world example of its use.
Mozilla used Rust to create Servo, an experimental browser engine (started in 2012 and first preview released in 2016), and its components were designed to run in parallel. Unfortunately, in August 2020, Mozilla laid off most of the Servo development team. Starting on November 17, 2020, Servo was taken over by the Linux Foundation. Some Servo features have now been integrated into Firefox.
Firefox Quantum includes Servo's CSS rendering engine. Rust has brought Firefox major performance improvements.
1.0.6 Rust Users and Case Studies
- Google: the Fuchsia operating system, with Rust accounting for 30% of the codebase
- Amazon: an operating system based on Linux that can run containers directly on bare metal or virtual machines
- System76: developed the next-generation secure operating system Redox entirely in Rust
- Stanford University and the University of Michigan: an embedded real-time operating system used in Google's cryptographic products
- Microsoft: rewriting some low-level components in Windows using Rust
- Microsoft: the WinRT/Rust project
Top comments (0)