Rust for embedded C programmers

I got a new laptop recently, so when a friend challenged me to write a little toy encryption algorithm, I needed to set up for writing code from scratch. Now that the world has basically standardized on Visual Studio Code as the IDE of choice, the Rust gang has been able to make solid strides in their already very sophisticated tool platform. Getting set up to compile and debug Rust in VS Code is nearly frictionless these days.

Doing math on character strings was a little more fraught, given my beginner’s level of understanding of the Rust type system. I hacked it out using a combination of The Book, Stack Overflow, and tenacity. It was a surprise to learn that unsigned overflow is not a given in Rust as it is in C/C++. Instead Rust has a huge suite of customized math operations that allow you to dial in the exact overflow behavior you want, enabled by the fact it is not object-oriented. What look like method calls are syntactic sugar for free functions that take a reference to “self”, which let them make massive libraries of functions on fundamental types. Java programmers, take note!

The struggles I had turning bytes to usize values and back to bytes stuck in my head. When I found this guide today for embedded C programmers, I read the whole thing with interest. It is nonsense-free and pitched right at me.

Among other things, today I learned Rust actually does have global variables. You still shouldn’t use them, but they are present. const variables look interesting too, like C++ consteval. I’d like to know more about that facility. This document about std::mem::transmute was also a fascinating look at how to do some of the kinds of things I was doing without invoking unsafe code.