Since the mid-1980s, C and C++ ruled when it came to low-level systems development. Then came Rust, released in 2015 after ten years’ development. Since then, it’s become the most-loved programming language on Stack Overflow’s annual Developer Survey… for multiple years in a row.
What’s behind that love? Here are five reasons why you might want to consider Rust.
Rust has Big-Company Support via the Rust Foundation
There are many programming languages invented each year, but you only hear about a few. The latter tend to be the ones with large company support behind them—think Dart (Google), C# (Microsoft) and Swift (Apple).
Rust was started by Mozilla and it’s now under the umbrella of the Rust Foundation, which includes members such as Amazon, Google, Huawei, Microsoft, Meta, Arm, Dropbox, Toyota and others. The foundation was started as a way to secure the future of Rust after Mozilla laid off 250 employees in 2020 due to the economic effects of COVID-19.
Rust Provides an Alternative to C++
It’s a bit like comparing apples and pears, but I suspect one big factor favors Rust: It’s a good fit for creating applications that you would otherwise have written in C++. This is because Rust eliminates classes of bugs related to C++ (for example, one C++ bug in 2016 allowed a cryptocurrency hack that resulted in million-dollar losses). Rust doesn’t have nulls, which are notorious for causing issues.
C++ is still faster at compiling source files, but if you touch any header, C++ is going to have to do a lot of recompiling. Fortunately, developers are working on improving Rust’s compile speed, helped by improvements to LLVM (the compiler uses LLVM for the backend).
Once you move beyond compiling single files with the Rust compiler, it’s best to use Cargo, Rust’s official build system and package manager. It checks dependencies and doesn’t recompile if there are no changes. This is definitely a big improvement over C++, which has no official build system (but at least seven alternatives such as Make, MSBuild, Ninja, etc.).
It’s significant that even Microsoft encourages development in Rust. According to Linus Torvalds, Rust will appear in the Linux kernel in 2023, making it the second official language for Linux after C. (Note, they are not going to rewrite all 25 million lines of Kernel C code into Rust, but to use it in areas like device drivers where memory management is critical.)
For Windows development, you still need C++ build tools or Visual Studio/Visual Studio Code installed, plus the Rust installer. There’s a crate for Windows API. A Crate is a Rust package; Cargo is used to download, compile, distribute, and upload crates. A Rust project is typically built up from many crates. Rust also has a package repository, Crates.io, with currently over 87,000 ‘crates in stock.’
Superior Memory Management
Rust has taken a leaf out of the C++ book by letting you control where and how memory is allocated and de-allocated. When you request memory for a variable, a smart pointer can be declared. However, de-allocation is done when that smart pointer goes out of scope. The approach is quite nuanced; you can also have multiple smart pointers that share the same data and are garbage collected. You can also access memory by reference, which doesn’t free memory when it goes out of scope.
Another facet of memory management is ownership, which is akin to move semantics in C++. This is done at the value level; every value has an owner.
Concurrency Done Right
The Rust official online documentation calls it fearless concurrency, which “allows you to write code that is free of subtle bugs and is easy to refactor without introducing new bugs.” This is quite a claim, as concurrency can normally lead to some very subtle bugs. The compiler prevents many of these bugs at compile time, making multiple threaded applications easier to develop without the kinds of hard-to-track-down bugs common in other languages.
It has Extensive WebAssembly Support
WebAssembly makes it possible to create video, audio, graphics, 3D environments, multimedia games, cryptographic computations, and even language implementations. Many languages support WebAssembly, but for production-quality code, it’s suggested you restrict yourself to C/C++, Rust or AssemblyScript.
It takes about two seconds to install the WebAssembly compiler into Rust. Compiling to WebAssembly is considerably easier than, say, for C or C++ using EmScripten. A rather neat example of Rust and WebAssembly is Ruffle, an open-source Flash emulator that runs as a desktop application or in browser using WebAssembly. It runs any existing Flash content, even on mobile.
Rust is now being used to build device drivers, embedded systems, emulators, games, and operating systems in many different arenas, including finance. This awesome list shows it being used for areas that ten years ago would have only been the domain of C++.
I haven’t mentioned Go, which some see as a rival to Rust. Rust is comparatively feature-heavy compared to Go. Go is a simpler language and unbeatable at compile speed, but if you want to write software that gets the most out of the hardware, Rust has the edge on execution speed and safety.
A major difference between newer languages like Python, Rust, Dart and older languages like C or C++ is their use of package repositories and a tie-in to the build systems. There are many package managers for C++, but no single official one just as there’s no official build system. This is a big strength of more modern languages.