My Adventures in Rust

The long journey to finding a programming language I could actually enjoy

Prologue

A while back in mid-2017, I decided to start learning raw OpenGL programming. The obvious choice to start this in was C++, since that was the language I had grown up writing in, and I had a pretty good idea of where to start. I began to lay out the framework of a GLSL demotool, similar to Shadertoy, and started hacking away. After a month or so of work, I had a semi-working project, with command-line arguments to load arbitrary shader programs and execute them in a window. Great!

The problem with the approach I was taking, though, was that whenever I needed to pull in another dependency for something, I needed to install it on my system and add the proper include paths to the compiler arguments. This is fine for generic dependencies that are present on most systems, like Freetype2 for font rendering, but when you need more esoteric libraries such as image loaders, you start to run into issues. How was I going to ever publish this if it took so much effort just to get a development environment set up?

I started looking for other solutions. Surely someone had come up with a suitable method of fetching build-time dependencies for C++, right? But the more I looked, the more hopeless it seemed. Sure, there were things like CMake, for finding dependencies and making sure everything gets linked properly, but what if I don't want to install everything on my system, just to run a silly shader toy? And what about cross-platform compatibility? Currently, I was just using straight up XLib and GLX for rendering, so if I wanted to port my project to Windows or MacOS, I'd have to rewrite pretty much everything.

After a bit of searching, I found a project that had piqued my interest a while back - an OpenGL accelerated terminal emulator called Alacritty. Alacritty is built with a very specific purpose in mind - to be the fastest thing out there - and it advertises its programming language, Rust, as being one of those things that enables it to be the very best. This, of course, got my interest right away. I was desperate for a new medium to express myself, so I hopped right on and started learning Rust.

So, what is Rust?

Rust is a relatively new programming language developed by Mozilla, and according to its website at rust-lang.org, is "a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety." Thread safety? Prevents segfaults? That sounded like something I could get behind, so I started to write some tutorial programs.

At first, Rust seemed to be just like C++, with a couple of quirks. Functions are declared as fn foo() {}, and variables as let foo = true;. Okay, pretty straightforward. There are also some tough concepts if you're coming from a C-like language. The Rust compiler tracks the lifetime of variables in order to efficiently free them when they are dropped from scope. It generally does this automatically, but it can get confusing for beginners when it needs some extra hinting. Regardless, this is a very useful language feature and one of the things that allows for efficient and fast execution without slowdown from garbage collection cycles, like JVM languages are notorious for. Coming from C++, Rust's type system was also very foreign to me. The concept of separating traits from data was difficult for me to wrap my head around, but in the end it's helped me a lot to write better code.

Advantages of Rust

After I had taken a few days to dive in and get myself situated with writing Rust code, I decided it was time to start reimplementing my demotool in this cool new language. Time to start looking for libraries! In Rust, projects are called "crates", and have a manifest named Cargo.toml that describes their dependencies and other metadata about them. Crates are uploaded to crates.io, and can be specified easily by other crates, just by adding them to the dependencies! No need to have extra packages installed on your system, since the build tool Cargo will automatically fetch, build, and link your dependencies for you!

Finding the crates I needed was easy, just a quick search of crates.io got me up and running with Glium. Glium is a cross-platform OpenGL library that works on Windows, MacOS, and any Unix with Wayland or Xorg running - much better than what I had been using before.

Rust's type checker and static analysis make development a breeze. Any developer will tell you that it's ten times easier to debug something at compile time than to struggle with a segmentation fault. I have only once suffered a segfault with Rust, and that was using the unstable nightly compiler.

Since the initial implementation, I've gotten Yotredash, the reincarnation of my original demotool, working on all major platforms with support for font rendering and other advanced features; as well as Vulkan rendering hopefully coming soon. It was honestly a breeze to work with Rust on this project, and any questions I had were quickly answered in #rust-beginners on Mozilla IRC. Rust has quickly become my all time favorite programming language for pretty much anything. It's good for whatever you need, be it low level USBHID protocols or even kernel development.