Learning a New Language in a Weekend (Rust)

The state of things

85% of my time programming over the last few years has been in C++, 10% in Python, and 5% in MATLAB. The thing is, I don’t like programming in any of these languages.

MATLAB is great for working on quick image processing tasks and big matrix operations. I used it quite a lot for a couple classes, and for some early prototyping of neural network designs. However, it would be far too slow for the video-oriented tasks I work on.

Python is… a language I loathe, to put it bluntly. While it’s great for rapid prototyping, data science, and machine learning, it’s only fast when Python code isn’t running. That is, you have to call on all sorts of packages which use underlying C code in order to achieve serious performance. That feels wrong to me, and it makes debugging a nightmare. I generally only use Python for machine learning programs with PyTorch, or for quick data transformation tasks.

C++ is fast, but it can be a nightmare to program in. You’re exposed to all sorts of memory bugs that can hide out for months and be extremely difficult to track down. Using #include statements is a nightmare without a package manager, especially when moving between machines with different library versions installed. CMake makes this a bit better, but getting that to work reliably is also a pain. The language is frustrating to write in, and it often got in the way of me making progress on my work.

My favorite language I’ve ever used is Elixir, a functional language for back-end systems designed for massive concurrency and resiliency. Unfortunately, Elixir is ill-suited to my research, so I haven’t touched it in a few years.

Enter Rust

A few months ago, I got fed up with C++. I wanted a change. I wanted to enjoy programming again, and feel empowered to work on the research itself, rather than have to spend so much time chasing down bugs and optimizations. A colleague of mine had badgered me about a language called Rust for upwards of a year, so I decided to check it out. My wife had a weekend shift at her work, so I was working through the weekend, too. I started reading the Rust Book on a Saturday morning.

I quickly learned that Rust can be as performant as C code, while providing some seriously impressive guarentees about memory safety through a borrow checker. Every piece of data has an owner, and there can only be one mutable reference to a value at time. This makes concurrency much safer, since the compiler strictly enforces these rules. Parallelizing a piece of code can be much more difficult in some cases than in C++, sometimes requiring clever organizations of struct members, but it’s a worthwhile tradeoff for the memory guarantees. I’d rather spend my time writing safe code than debugging unsafe code. Furthermore, it has a simple, modern package manager provide by the Cargo system.

By Monday morning, two days after starting to read the documentation, I had rewritten the bulk of my exisiting code base for asynchronous camera emulation entirely in Rust. It was that easy. The kicker? It was already faster than my C++/CUDA implementation. Upon having that realization, I was converted. For the last couple of months since then, I’ve done all my further development in Rust, and worked to further optimize it as I learn new features of the language and discover helpful packages. Admittedly, there are some things that continue to cause me headaches (maybe I’ll write about them later), but all in all this has made my work life much more enjoyable. I hope to release my new Rust codebase sometime this summer, once I get it packaged more nicely.