dark mode light mode Search Menu
Search

Rust Shines Bright

Sergei F on Flickr

Of all the many programming languages out there, few are suitable for systems programming, that is writing code that’s able to run as the basis of an operating system or on tiny pieces of hardware like microcontrollers. The main language used for these tasks is C, one of the oldest languages still in use. In the past decade, however, another language is starting to shine.

The Rust language for systems programming has managed to do something really special —make it easier to write code without a runtime system. Other than C, it is the first of its kind to be included in Linux’s code base.

Gotta Run

As background, most programming languages have a “runtime system”: a bunch of code that gets attached to the final program once it’s been compiled in order to handle things like creating and destroying data or making space for variables and getting rid of it when no longer needed. This is important because usually in a language that doesn’t have a runtime system you have to do those things yourself, which can lead to mistakes in allocating or freeing memory, and cause all sorts of problems.

However, a language you’re going to use for an operating system or on a little tiny board like an Arduino can’t afford to have this code-intensive runtime system, especially since it sometimes has to pause running the program for a fraction of a second in order to handle all these tasks.

For most applications, that’s not a problem. But can you imagine your mouse —or even worse, the software running a car or a robotic arm—stuttering because the code running the operating system needs to figure out what it’s doing with memory?

Rust manages to accomplish this kind of give-and-take between the compiler and the programmer. The compiler has the ability to check and make sure that memory is properly allocated when needed, and freed once the data is no longer used in the program. Rust doesn’t need a runtime system because it figures out all the stuff that needs to be done with memory when you compile the program and then inserts all the code to handle memory for you. So, you don’t have a runtime system but you also don’t have to run it yourself. It’s the best of both worlds.

Rainmaker

What you do have to do in Rust is write code a little differently than you normally would in most languages. In a language like, say, Lua—as you’ll see in the TIC-80 article this issue—you can have a variable that stores a list of all the things that you’re going to move on the screen, raindrops for instance. Then, all the functions you write can just take this list of raindrops and do things like move them, show them, or erase them when they exit the screen.

In Rust, you can’t do things like that. There’s a concept of “ownership” for data in Rust, and if you hand a function some data (like our list of raindrops) it can’t just change the list of raindrops, it has to give it back as well, or else the list is destroyed at the end of the function. If the last function to own the data finishes without giving it to another function or returning it, then Rust won’t let you use it anymore. If you try, then the compiler tells you that it can’t do its job of handling memory and it won’t finish making the program.

There are other things in Rust that are different from other languages. For example, you have to declare whether a piece of data is allowed to be changed later in the program. There’s also a notion of functions “borrowing” data. This doesn’t give them as much control as if they owned the data, but it also means you don’t have to give it back: it’s returned to the owner automatically.

That may sound like a lot, but it’s really just a change in how you write programs and not even a big one.

One of the great things about Rust is that it has some really amazing tutorials and references. The official book is free online, there’s a great tutorial on both learning Rust and making video games, and a really cool creative coding library that lets you make generative art pieces.

In the end, Rust is an interesting, even exciting, young-ish programming language, and knowing it may open doors to all sorts of opportunities.

Learn More

Rust site

https://www.rust-lang.org/

Rust book

https://doc.rust-lang.org/book/

Creating Roguelikes in Rust

https://bfnightly.bracketproductions.com/chapter_0.html

Nannou: creative coding in Rust

https://nannou.cc/

Systems Programming

https://devopedia.org/systems-programming

Best systems programming languages

https://www.slant.co/topics/6032/~systems-programming-languages