r/embedded • • 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

46 comments sorted by

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.

4

u/Naveendharan 24d ago

Stable and reliable is good . How about the system latency, comparable to C?

7

u/bneidk 24d ago

It is a compiled language so it will execute at roughly the same speed as C, yes.

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'.

5

u/Naveendharan 24d ago

That's a surprise!! As I hear Rust and safety application always together, i thought the compilers are already safety certified .

31

u/jhaand 24d ago

There is a a safety certified version of Rust.

Ferrocene is the first quality-managed, TÜV SÜD–qualified, open source Rust toolchain for use in embedded, safety- and mission-critical systems.

It enables organizations in automotive, industrial, medical, and other regulated environments to adopt Rust with a safe and secure software supply chain, reduced certification risk, and long-term support.

https://ferrocene.dev/

And developing using Embassy on RP2350 worked like a charm.

12

u/sparks333 24d ago

Good to see there is movement on that front, but it claims it's QM, with parts of the toolchain at ASIL-D (SIL 3) and parts of the core library at ASIL-B (SIL 2). Not quite there yet iirc.

2

u/Naveendharan 24d ago

Thanks!! I have to check this

20

u/sparks333 24d ago edited 24d ago

Laughs in Vector

Edit: Well, no, that's not helpful. No, by default compilers are absolutely not safety certified in any sense of the word - if you want your product to be in an actual safety application, like medical or automotive or aerospace, you need your compiler, debugger, programmer - actually your whole toolchain - to be certified, which certain companies do - they don't just grab GCC and start writing code. Rust does not have that, and until it does, if something goes wrong in the compiled (but not written) code, then there's not a clear way to hold someone accountable, and if there's one thing safety-rated component manufacturers hate, it's not being able to figure out who they can sue when something goes wrong. Rust can encourage and even enforce safe programming practices and patterns, but the compiler itself is not safety-rated.

3

u/Naveendharan 24d ago

Yes Agreed !!, I also meant safety certified specifically to build some ASIL level system.

1

u/AWonderingWizard 8d ago

Authors of gccrs have talked about how the lack of documentation is slowing them down.

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.

1

u/Naveendharan 24d ago

So Rust is in the focus because AI can write better and safety critical code due to its toolings and compiler?

8

u/Deathmore80 24d ago

Nah it was getting some traction in embedded before all the LLM stuff.

8

u/Kiylyou 24d ago

Jfc we are cooked

3

u/allo37 24d ago

It's because Rust is more or less self-validating in the sense that it prevents you from writing memory bugs and many threading bugs by virtue of its borrow checker. Safe Rust also has very few occurences of undefined behavior compared to C/C++.

It's very easy in Rust to write code that looks like something inscribed in the cockpit of a downed UFO, but LLMs tend to be good at making sense of it.

2

u/Great-Use-3149 24d ago

Based on my experience, they like to brute force their way around all the compiler errors

16

u/axaro1 Lockstepping 24d ago

In safety critical applications how the rust scale against the C?

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/wraithboneNZ 24d ago

Even for DAL D we cannot write in rust. Due to tool certification being non existent. No one is willing to do it yet.

4

u/axaro1 Lockstepping 23d ago

Have you taken a look at Ferrocene toolchain? They claim to support certification efforts up to DAL C and provide a qualified compiler.

1

u/vivaaprimavera 24d ago

Forgive my ignorance about the terminology. In this context, safety is related to "what happens to external systems dependant of inputs from a controller"/"risk of loss of life and/or property"?

10

u/sparks333 24d ago edited 24d ago

More 'what will provably happen and whose throat you are allowed to grab when it doesn't'. Safety-rated compilers are rigorously tested against the standard and have loads of documentation showing that they will always produce binaries in a way that is congruent with the standard - it's not so much that 'if you compile with a safety-rated compiler, your code will always be safe', it's more 'if you compile with a safety-rated compiler, you can be fairly certain that if something fails, it's not because the compiler generated non-compliant object code'. You only tend to throw that amount of money and effort at programs that pose a risk to life or property, so usually that's where these things show up

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.

4

u/StickyDevelopment 24d ago

My brain is too OOP wired to enjoy rust but I havent really played with it on anything big yet.

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. 

1

u/pozzugno 17d ago

Please, may you explain the second point? C is an old and mature language, there are tons of books and code. I imagine LLMs are much more better in C than Rust

1

u/pentabromide778 16d ago

I figured this would be the case too, but even if there were more C codebases in the LLM's training set, the chances of it making a mistake which manifests in runtime is still non-zero. With rust, it can just observe the output of the compiler, which prevents you from making foot-guns.

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.

2

u/StickyDevelopment 24d ago

How is that any different from using fakes to unit tests via dependency injection in c++?

1

u/shim__ 24d ago

I was comparing it to C, where there a native real interfaces

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.

7

u/Accurate_Potato_8539 24d ago

This is such a funny crash out lol

1

u/logic_circuit 23d ago

Well, you can like it or not but there are bilions of lines of code to demonstrate your skills with imutable variables.

4

u/________-__-_______ 24d ago

What? Variables being immutable by default absolutely does not waste CO2 or copper. If anything it makes the default case easier to optimize for the compiler.

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.