Rust for STM32

What follows are general notes from research on whether Rust is currently viable on STM32, as well as a comparison to C.

Terms/info that might be unclear in this doc coming from C (to update if/when doc expands):

Term

Translation

Term

Translation

Crate

Rust packages/modules. Similar in use + management as pip packages

High-level pros/cons of switching to Rust

Pro

Con

Pro

Con

  • Better error catching at compile time (e.g. throws out-of-bounds exceptions on arrays)

  • Catches some race conditions

  • Same/better performance as C

  • Can compile + interoperate with C code in the same program, making porting over current fw easier if possible

  • Low level register access available as well as general HAL:

  • Really good documentation + tutorials for all aspects, making learning/debugging easier

    • See here for embedded Rust resources

  • Nicer build system

    • SIGNIFICANTLY better library/dependency management with crates (similar to how python modules work from what I've seen).  This shouldn't matter much for our usage, but it's a nice touch

  • Automated documentation through rustdoc

    • Way more readable than comments; makes it easier to read through a bunch of modules you haven't written and get a sense of what they do and formatting is nicer

    • Seems like there’s some similar modules to this for C, but this is better integrated

    • Can have test code as a part of this if we want. Not sure if it’s possible to have the status of tests for each module on these pages

    • Can view source code online through this as well

  • Nicer error messages than C

    • I don't really have anything to back this up with, but seems like the prevailing sentiment is that preventing undefined behaviour and the like means that error messages tend to be more helpful

  • Less in-demand than C for embedded co-ops/jobs

    • Probably easier to go C -> Rust than Rust -> C since C compiler will catch fewer errors

  • Fewer people are likely to have Rust experience than C/C++ or similar languages

    • Made easier by significantly better documentation + tutorials than anything I've seen for C embedded or C in general

    • See here for a general list of Rust resources

    • A lot of people joining MS will probably get experience in writing cpp in first-year (at least for eng) if they don't already have it, but I'd be surprised if anyone has any Rust experience

  • Can mark any code as "unsafe" to get past most compiler checks (pretty much everything that makes Rust an improvement over C from what I can see), removing some of the advantages of Rust's strict compile-time restrictions.  This is necessary to develop some embedded FW but probably won't be used often at a higher level than the HAL from what I'd guess from taking a brief look at some Rust embedded applications

  • Would likely have to write CAN API. Seems like we had to do this anyways for C?

  • Embedded Rust modules are still under development.  Seems like they're working towards a stable release across all modules, but that looks like it's not going to be for a while.  As it stands right now, we might end up writing FW for a module with bugs and then have to port everything over to the stable release, which will probably be API-breaking

    • See this issue for more info regarding the 1.0 release

    • Seems like this only applies to the cortex-m crate we’d be using and the others don’t even have plans to push a 1.0 release

    • I’m planning on looking into this further if we decide to pursue Rust more: I feel like some of the notes I wrote on modules might not be entirely correct but it’s difficult to know for sure without actually going through the process of flashing Rust FW to a target

  • Longer compile times than C

Other notes

Generally, seems like porting to Rust largely depends on whether most members want to switch. There are some advantages with the build/test framework compared to C, but a lot of the pros that I’ve found are related to Rust supporting things that C already supports.

C should be much more desired by employers than Rust for embedded (and in general ATM).

We’d likely be using the tooling documented on this page.

I’m not sure how feasible CI is with Rust embedded compared to our current build system. Like most things with embedded Rust, it’s definitely doable, but it may take a lot more work than with C.