Rust Shines Bright
Image by 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.
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.
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.
Creating Roguelikes in Rust
Nannou: creative coding in Rust
Best systems programming languages
Also In The February 2023 Issue
If aliens exist, why haven’t we met any yet?
Learn how the origins of cybernetics may pave the way for the future of machine learning
Don’t let the name fool you, this systems programming language is anything but rusty!
With every end, there is a new beginning. Especially in creative robotics!
Don’t forget your umbrella for this TIC-80 Lab
Save time with this neat OpenSCAD shortcut
Time to get inspired by mother nature!
Learn how drones are being used to repair the Great Wall of China and find ancient structures!
These robot mice move like mechanical cats are chasing them!
Try out this Zombie Survival mod for Minecraft if you dare!
This trash loving AI joins the fight for a greener earth!
Spice up your 3D-printed food with time!
Now that you have made your masterpiece, it is time to share it with the world.
Time to get your Farming robots’ hands dirty!
Be the FIRST at your school to join this awesome program
Links from the bottom of all the February 2023 articles, collected in one place for you to print, share, or bookmark.
Explore the scientific origins of memes!