Rust is a multi-paradigm, high-level, general-purpose programming language that emphasizes performance, type safety and concurrency. It was developed by Graydon Hoare and Mozilla Labs in 2010. According to The Rust Book , "The Rust programming language is fundamentally about empowerment: no matter what kind of code you are writing now, Rust empowers you to reach farther, to program with confidence in a wider variety of domains than you did before". Let us now discuss how Rust is a tool for empowerment and why it is often referred to as the programming language for the next 50 years.
Wikipedia defines systems programming as "The activity of programming computer system software. The primary distinguishing characteristic of systems programming when compared to application programming is that application programming aims to produce software which provides services to the user directly (e.g. word processor), whereas systems programming aims to produce software and software platforms which provide services to other software, are performance constrained, or both (e.g. operating systems, computational science applications, game engines, industrial automation, and software as a service applications)".
Systems programming is often associated with mission-critical programs that either manage other programs or is depended upon by other programs, and are closer to the hardware than their application counterparts. Imagine web browsers, game engines, operating systems and embedded systems, programming language compilers/interpreters, etcetera.
For decades, C and C++ have dominated the systems programming world. Think of the Linux kernel, the Mars rovers, the Unity game engine, and other important systems software; C/C++ are the de facto languages used. However, C and C++ are quite difficult to work with, and software developers have been seeking better alternatives for decades. Much of the difficulty that comes with using them is manual memory management. Remember when Bjarne Stroustrup, the legendary creator of C++ said "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off". This was a reference (pun intended) to the delicate art and science of memory management.
With C and C++, you have the ability to manually allocate, and de-allocate memory for your programs. This is necessary for all software that need to dynamically allocate chunks of RAM resources to be used for temporarily storing data during code execution. The catch; you have to de-allocate memory you previously allocated, at the right time, in the right place, and you must NEVER use memory you previously de-allocated. You also must be careful about access to memory by different parts of your code. Certain parts of your code must not have access to read or write to certain memory being used by the program. This sounds like a complex task, but it is even more complex than it seems, especially for large systems software with millions of "moving parts". Several researches have been conducted, and it was found that memory safety bugs are a major cause of security vulnerabilities in modern software, as elaborated in this article by Microsoft, and this article where Rust was presented as a possible solution to those problems.
Of course, with great power comes great responsibility. The great power that C and C++ offer comes with the caveat of wielding them with caution. In reference to the quote by Bjarne Stroustrup, what if I don't ever want to shoot myself in the foot anyways? Unlike C and C++, Rust makes it harder to write incorrect, buggy, or memory unsafe software. Many memory safety bugs seen in C/C++ codebases are even outright impossible to replicate in Rust.
Below is a tweet by the CTO of Microsoft Azure:
Speaking of languages, it's time to halt starting any new projects in C/C++ and use Rust for those scenarios where a non-GC language is required. For the sake of security and reliability. the industry should declare those languages as deprecated.— Mark Russinovich (@markrussinovich) September 19, 2022
In Rust, memory management is primarily done through a system of ownership and borrowing, which is enforced by the borrow checker at compile time. This ensures that memory is managed efficiently and safely at runtime, without the need for a garbage collector or manual memory management.
When a value is created in Rust, it is assigned an owner, which is responsible for managing its memory allocation and deallocation. The owner can then lend out references to the value, which can be used by other parts of the program. These references are known as borrows, and can be either mutable or immutable. The borrow checker ensures that there is only one mutable borrow at a time for a particular value, which prevents data races and other unsafe behavior. It also enforces a set of rules for how borrows can be used and when they must be returned, which ensures that memory is not accessed after it has been deallocated or when it is no longer valid.
In addition to ownership and borrowing, Rust also includes a number of low-level features for managing memory, such as raw pointers and unsafe code. These features can be used to interact with memory directly, but they require careful consideration and are typically only used in performance-critical or system-level code.
In essence, you can use Rust almost as you would use a programming language like Go or Python where memory is managed by a garbage collector, but without the performance consequence of a garbage collector. The big difference between Rust and C/C++ is that Rust is safe by default; all memory accesses are checked at compile time, but you also get the performance and precise-level of control required for low-level programming, and high-level abstractions required for high-level programming.
Rust's strongly typed system which makes it nearly impossible to have runtime errors, large collection of libraries and frameworks, portability, and many other features all make Rust an excellent programming language for all kinds of software, even for non-systems programming like web development and game development.
"Our civilization runs on software" - Bjarne Stroustrup. Software depends on electricity, and In 2019 almost two-thirds (63.3%) of global electricity came from fossil fuels. Global warming is one of the deadliest threats to the human species. Our continuous use of fossil fuels, and emission of greenhouse gases is both dangerous, and unsustainable. We however cannot just stop the emission of greenhouse gases, because a lot of research and investments have to go into the generation and distribution of renewable energy. There have been technological breakthroughs, and government investment in renewable energy is increasing, but while we are not there yet, what can software developers do to reduce the electricity consumed by the software we ship to consumer devices and cloud infrastructure?
Just use Rust!
Okay maybe use a different programming language but hear me out; Our code needs to run somewhere. Mobile phones, AWS or Google Cloud servers, satellites in space, and even small places like Airpods and IoT devices. All these computers consume a lot of electricity to process our code and do the things we programmed them to do.
The New York Times said in an article: "In 2018, data centers consumed about 1 percent of the world’s electricity output. That is the energy-consumption equivalent of 17 million American households, a sizable amount of energy use". Big cloud providers need an enormous and constant supply of electricity to power their servers that keep the internet running.
As programmers and software developers, what if we optimize our code to use fewer and fewer resources? What if your Lamda function takes only 1 second to run instead of 2 seconds? Then fewer CPU cycles are used to run it, and fewer propeller rotations are used to cool that CPU, and much less electricity is required to keep that cooling system running.
What if the games we ship to billions of mobile phones do not drain the battery of the device in the first 10 minutes of play, such that these phones have to be recharged so frequently? What if such games were made more performant and battery efficient? These optimizations can be made in software without modifying any hardware.
Well alright, these optimizations can be done in any programming language, but with Rust, your code is performant and optimized by default. The worst performing Rust code can be compared to the best performing Python code doing the same task. With Rust, you rarely have to think of performance, and when you do, it is easy to achieve decent performance simply by tweaking the code or changing compiler flags.
In a bid to reduce their carbon footprint, AWS (the largest cloud computing provider) developed Firecracker, an open-source virtualization technology written in Rust that powers AWS Lambda and other serverless offerings. Check out this article by AWS on how they are reducing their energy costs and carbon footprint with Rust. IBM , Microsoft , Meta , Google, and Apple are all working together to move the cloud towards a sustainable future, and Rust is a major factor in their efforts. Use a blaaazzzingly fast and energy efficient programming language like Rust :)
"WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications." - webassembly.org.
Rust is heavily used for writing decentralized systems and blockchain applications. One of the first implementations of Ethereum was written in Rust, and smart contracts for the Solana and Aptos blockchains are also written in Rust.
Rust is also used even more actively for the infrastructure surrounding blockchain and web3 applications like moralis and IPFS.
Rust is becoming a prominent game development language. I have done a few experiments with Rust game engines like Bevy and Fyrox, and also used Rust bindings for scripting in game engines like Godot. It was a fun experience, and i was surprised how far Rust has come in the game development space.
Game backends and multiplayer servers are also excellent places to use Rust for game development. I have personally written very many multiplayer servers in Rust. See Are We Game Yet to know the current state of game development with Rust.
The Rust Book is a good place to start. It is a good introduction to everything Rust, and will lead you on the adventure of becoming an official Rustacean!
Thank you for reading,
Give me a follow on Twitter to get updated on my new articles, and join my Discord server if you would like to see cool open-source projects i am building with an awesome community of software enthusiasts.
Consider donating, or becoming a sponsor to help me transition into making open-source software, full-time.