En rostig bil i öknen

Rusty code is good code!

Magnus JonssonUtveckling4 min läsning

Since the dawn of time (i.e., around 1970), C, and later C++, have been used to build both small programs and large systems. The languages are fast and work on virtually any device. Everything from operating systems and computer games down to the display on your washing machine is often written in C and/or C++. But now, perhaps this is about to change?

Contents

A majority of all security vulnerabilities that are discovered (and also exploited by hackers) are not the ones we usually talk about. DDoS, Phishing, XSS, SQL Injection, etc. can cause great damage to those affected, and when attacks succeed, they can make big headlines. But these are still in the minority. Most security vulnerabilities instead come from improper memory management in programs. The creators of Rust want to change this.

Memory-safe code

On February 24, 2024, the White House issued a report recommending the use of memory-safe programming languages. This is thus no longer a narrow discussion among developers but a topic that the US government is getting involved in. But what is it about then?

All computer programs, big and small, need memory. In memory, various variables and data sets are stored. Programs running on the same machine have then stored information in the same memory but with different addresses. However, things can easily go wrong here. Say a program saves a list of 20 data points in memory. Then the program tries to read information at position 21. Here, your program could then read data that actually belongs to a completely different program. Somewhat simplified, this was essentially what happened in 2014 when "Heartbleed" was discovered, where, among other things, millions of patient records in the USA were stolen.

This is where memory-safe languages, such as Rust, come in.

Could I borrow a cup of memory?

To prevent the program's memory from being used incorrectly, Rust's compiler will go through all code and ensure that all memory is handled correctly. This is done by requiring each part of the program's memory to be owned by a specific function. Another function can take over ownership, but then the first function can no longer read or write to that memory. As soon as memory is no longer owned by a function, it disappears automatically. Functions can also borrow memory from each other. This must then be programmed correctly so that the memory can be handled securely.

All of this is handled by Rust's compiler, more specifically by the compiler's "Borrow checker". If you've made a mistake somewhere or forgotten to declare that in this particular case the memory should be lent here or there, your program will not compile. This will happen often as a new Rust developer! It's not uncommon for developers to complain that they have to wrestle with the Borrow checker, but even if it can be frustrating, it's because code that could lead to bugs or security vulnerabilities has been detected.

It is, of course, possible to do all of this correctly in C and C++, but it requires a lot of time and experience (even experienced developers make mistakes sometimes!). With Rust, you get all of this from square one!

Safe, check! But otherwise?

There are many memory-safe languages. For example, Java, C#, and Python are significantly more popular than Rust. So why switch to Rust?

The languages above all have one thing in common: They require a special runtime just to be able to execute. To run a program written in Java, you must have the Java platform installed. That platform handles memory allocation and removes memory that is no longer used, so you as a developer don't have to think about it. Depending on where the code is to be run, it's not certain that the platform exists or can even be installed. Rust, just like C/C++, can run virtually anywhere.

Rust is also a very pleasant language to program in. Rust inherits many features from functional programming, which makes state management significantly easier to work with. Other goodies like pattern-matching, traits, and a total absence of null make the written code easy to read, understand, and reuse. Rust's own linter "Clippy" helps you write correct and structured code. Furthermore, the existence of a large library of ready-made packages to include in your code is the icing on the cake.

This is, of course, a highly subjective opinion, but many agree! Data from Stack Overflow's 2024 survey shows that Rust is the most admired language today.

So, what are you waiting for? Read more about the language and how to get started on Rust's website!