Rust as a language for a beginner in embedded programming

Hi. I already have programming experience and recently I got involved in two things at the same time: Rust and embedded programming (just for fun). I have experience with C but haven't used it for embedded programming. Is it worth it to start learning bare-metal programming right away by writing Rust code, or is it better to learn it by coding in C? Will additional abstractions and tools in Rust interfere with understanding bare-metal programming and is better to start with "bare" C?

It depends on what you want to do. The C toolchains for various embedded targets will be more mature. The rust embedded crates are still under heavy design and development work. It's fun to work with them but if you want to build a commercial product with longer support, it can be painful if things keep changing under you.

I don't think you need to be afraid of that, embedded Rust is not any less "bare" than embedded C, they live at similar levels of abstraction.

At this stage, it's just a hobby. Probably, I would like to dive into bare-metal programming without using ready-made tools and libs like HAL and so that the language is low-level and transparent enough so that I can understand how it works on this microcontroller/CPU. I see that C is a priority candidate, but I would like to practice this using Rust. How much better/worse/harder would this be?

There is nothing special about "bare-metal" programming. It's just programming. Just do it.

Except of course Rust is fussy about use of memory that is out of it's control. Which will likely include processor/peripheral registers and such. That is OK, that is what "unsafe" is for.

As far as as I can tell one can get a long way ignoring the higher level abstractions of Rust and hitting the metal directly, as one does in C. Ultimately Rust and C compile to pretty much the same thing thanks to LLVM.

It might be better to get familiar with programming ones target device in assembler first, skip the C idea.

I know the embedded Rust guys want to wrap every bit field of every peripheral register in some HAL abstraction. That is is great and all but, as all such abstractions do, it obfuscates what you want to do as a beginner.


Right, you don't need to use any of the HAL crates and abstractions and other pre-existing support crates (though for tsay, low-level startup code, it's convenient if there's some CPU architecture crate that you can already use).

If you want you can use unsafe rust and simply poke into memory and memory-mapped registers with pointers, just like with C. With nightly rust you can use inline assembly as well.

1 Like

If you know C, you probably understand enough about memory and pointers to understand embedded in either C or Rust. Most chips would probably have C support libs written by the manufacturer, and ARM chips can also use things like mbed, which is a C++ rtos.

However, if you just want to get a microcontroller on a dev board do some IO and flash some LEDs, read/write some pins and use some of its peripherals to communicate with other computers or digitize analog inputs as a hobby, then Rust is probably a good choice. If you have deadlines and need to guarantee production stability, I'd stick to the C libs for now.

1 Like