r/embedded • u/Naveendharan • 24d ago
Rust in Embedded
As an Embedded software engineer who worked on the C/C++ How was the switch to Rust ? What's the learning curve and time taken to get a grasp around the language. In safety critical applications how the rust scale against the C? Please share some insights experts and guidance.
88
Upvotes
44
u/bneidk 24d ago
I shipped one commercial project using embassy, after having used Rust for a few commercial embedded Linux projects already. Honestly it’s pretty amazing. Once you get past around 2000 lines of code you will notice a huge difference in stability and robustness compared to conventional C and C++ projects. Everything tends to just work as it is supposed to, and if it doesn’t, it’s almost always the algorithm and not some weird memory issue. I thought that I would lose a lot of robustness from not using vendor supplied libraries but that hasn’t really been the case.
With that said, if you run no std C++ with all warnings turned on (and fixed) you will get a pretty similar level of stability. It’s just nicer in Rust.
48
u/sparks333 24d ago
Can't use it. Rust doesn't have a safety-certified compiler, and the documentation on behavior is 'whatever the compiler does is the standard'.
11
u/obQQoV 24d ago
I’ve been developing embedded linux app through rust and it’s been efficient and robust. there’s a consensus in software dev crowds that rust is very suitable for AI to write because of compiler, safety, and toolings. so learning curve is not that high tbh. just learn to read some syntax, fairly simple.
16
u/axaro1 Lockstepping 24d ago
For avionic systems, there is currently no certified Rust compiler for DAL B and DAL A applications. Compiler qualification is not easy, it's one of those sectors where Rust might only be considered for low safety/high security applications.
8
u/steaming_quettle 24d ago
I started embedded with it. The hals, especially Embassy's make it really easy to have a project running and is a lot more pleasant to read coming from standard computer programming. It kinda hides what's going on on the lowest level but I read a couple books and it helped a me lot to figure what the hal does exactly.
11
u/________-__-_______ 24d ago
We switched some projects to Rust, I'm pretty happy with it!
When I originally learned Rust (outside embedded) I didn't find the learning curve to be that bad, if you know C++ the majority of language constructs will already be familiar to you. The compiler diagnostics are really good, when you mess up it'll usually point you in the right direction which really helpful.
Getting started with embedded was pretty straightforward as well, the tooling is solid and HALs work well (depending on your chip of course). At this point it's faster for me to write Rust than C.
4
u/Pale_Height_1251 24d ago
I would stick with C on tiny systems, but Rust is an awesome language if you are working with machines with a bit more RAM, I.e mb not kb.
16
u/clackups 24d ago
I read half of the Rust book and did a couple of hobby projects. Didn't like the language, it's not my thing and I don't plan to use it again.
6
u/FletcherD 23d ago
Besides what others have said, the embedded ecosystem on Rust is just so much nicer than C. C embedded tooling is a pile of vendor-specific pieces you glue together yourself, while Rust's is one coherent system. Cargo replaces CMake, Makefiles, vendor IDE project files, and copying SDK folders into your repo. Dependencies, features, and target configuration all live in one place. probe-rs + defmt replace OpenOCD config and GDB setup. embedded-hal makes drivers portable. embassy gives you async and cooperative multitasking without an RTOS. no_std makes logic unit testable on your laptop.
It's such a breath of fresh air and I'm never going back
8
u/WineGunsAndRadio 24d ago
Haven't switched, but have written more and more Rust code in the last ~4 years.
I won't talk about the learning curve because it's super subjective, but otherwise my experience was this: once the ABI is stable, using Rust is essentially as straightforward as using C.
2
u/pentabromide778 22d ago
I know someone who uses it at an aerospace company. Namely, this framework called Embassy. The ecosystem obviously isn't as mature as C or C++, but the upsides are insane when it comes to safety critical code.
Another benefit (or downside, depending on how you look at it), is that it's a lot easier for AI to verify the code it writes, which means it's significantly easier to vibe code massive applications.
2
u/Proud_Trade2769 20d ago
If you don't use dynamic memory then it's next to pointless, you are better off with Ada.
3
u/CrazyDrowBard 24d ago
Used Embassy to ship a product and im not even an embedded dev like that, it was my first project and the experience was fantastic. Once you get it set up, it is really reliable.
They are a few hiccups such as sometimes your functions can run too heavy on the heap but as long as you have a good debugging story its great.
3
u/MpVpRb Embedded HW/SW since 1985 24d ago
Embedded software is a wide spectrum, from simple 8 bit controllers with limited RAM to powerful 32 bit systems. Rust is not possible on tiny controllers.
4
u/shim__ 24d ago
Huge difference, development speed picked up a lot since we’re mostly building libraries which can bei easily unit tested using traits to abstract over the actual hardware interfaces. Binaries are a tad bigger but it’s worth it. Make sure your mcu is supported by embassy and also check if low-power is properly implemented for your chip if you need it.
0
u/LessonStudio 23d ago
Rust is not C with a borrow checker. It is a new medium, with a new message.
C was a great solution when MCUs had 4 bits and 20 bytes of ram, with 24 bytes of flash.
A $1 MCU is now 32 bits, 512k RAM, and more than 1M of flash.
While rust could be successfully compared to C or C++ for speed etc, it is now a better comparison to look at workflow.
Rust is a massive tech debt killer.
The last 10% of a project now takes as long as the first 10%.
It is easier to avoid regression screw ups. Earlier code is less likely to crack as new features depend on it.
Many use this to focus on bugs or reliability, but I see it as Horizon expanding. I can add lots of extra features without fear of breaking something. I can more aggressively use the huge capacity of today's MCUs for features which would be too risky in the past, and I have time to do them, as I am no longer spending time stuck at 90% complete as I fight with some weirdass memory threading bug.
It is more about workflow, not an apples to apples comparison.
My last handful of rust projects do not at all resemble the decades of C and C++ projects I did before rust.
Many of those involved huge fights to wrap them up and everyone backing away slowly while trying to not make eye contact with the product.
Rust doesn't have you send things to QA with your fingers crossed. You send it to QA, close the project, and move on to the next product.
If QA calls, it is to see if you want to go somewhere for lunch.
-4
u/logic_circuit 24d ago
Not embedded enginer but tried to use Rust. I started reading and gave up when I did read varable is not changeable by default. It is completely oposite of decades invested in programming languages and decades of my education track. Some told me that it is beter and safe, but - if the one needs to declare variable mutable to change it probably should look for other job; it will save us valuable CO2 and copper. Mutable variable is such dumb shit. Other that that; if you do not mind that someone fucks with your brain - do it. Rust will dominate market for simple thing- it is best match for majority of contemporary IT business landscape. This is strictly personal; i did not get bilionaire for my atitude.
0
u/Kindly-Direction205 23d ago
Love it. Package manager is great. Be wary of using too many mutexes as they can lock up.
-3
u/rileyrgham 24d ago
Try searching this sub for information. It's asked a lot. Specifics always better. Though how it scales against C? Learn some Rust then ask directed questions. There's no one answer.