Home Articles Tutorials Videos Projects
Why you should learn Rust

Why you should learn Rust

10 Apr, 2023

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.

The systems programming dilemma

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.

A case for Rust ๐Ÿฆ€

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:

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.

Modern software is now being written (and re-written) in Rust, since Rust programs are usually more reliable and performant than those written in other programming languages like Go, Python, JavaScript, etc.

Rust is saving the planet ๐ŸŒ

"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 :)

Rust on the web ๐ŸŒ

We know Rust as a blazingly fast systems programming language, but what can Rust do on the web? Rust has turned out to be an excellent language for web development. There are many frameworks for frontend and backend development. Actix web, Yew, Rocket, Warp, and many others. The role that Rust plays in modern web development is immense. I have written web servers in multiple languages including Go, C, C++, Javascript, C#, Python, and many others. Why I think Rust is unique is that; once my code compiles, it never breaks. I have also seen significant performance improvements in my web projects just by rewriting them in Rust. This website, as you read this article is written entirely in Rust, but that was not the case a few months ago. It was originally written in Go, but after rewriting it in Rust i saw major performance gains, and i paid less in server costs. You currently may not use Rust directly for web development, but you actually do so by proxy. If you use Cloudflare, you or your users use Firefox, or you use AWS for hosting/serving your web services, then you use Rust by proxy. See Are We Web Yet to know the current state of web development with Rust.

Rust and WebAssembly ๐Ÿ‘จโ€๐Ÿ’ป

"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 arguably the best language to use for writing WebAssembly applications. You can generate smaller wasm binaries and create more reliable programs with Rust's rich type system. Check out this article i wrote about WASM, Javascript, and Rust.

Rust in web3 ๐Ÿ”—

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 for game development ๐ŸŽฎ

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.

Getting started 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.

this website does not show ads, track you, or collect your data. have a great day :)