3 Causes to Use Rust in Embedded Methods

3 Causes to Use Rust in Embedded Methods

There has at all times been a fierce conflict being waged between C/C++ for the hearts of embedded tool builders. The conflict has lasted many years, with C++ slowly however no doubt whittling down the choice of embedded techniques constructed the usage of C. C continues to be the most well liked embedded language. Nonetheless, the controversy over which language to make use of has intensified in recent times. What’s maximum fascinating is that it’s not a debate between C and C++. As an alternative, there’s a rising development and drive throughout the tool business to make use of Rust. This publish will discover a number of the reason why Rust may well be a good selection to your subsequent embedded tool programming language.  

Explanation why #1 – Rust is a memory-safe language

Have you ever ever written a C/C++ software that had a reminiscence leak? Or written an software the place you destroyed an object simplest to be told later that the article used to be nonetheless in use in other places within the software? What about liberating the precise reminiscence location two times or by accident overwriting the limits of an array? To be frank, I’ve executed these kind of issues and worse at one level or every other all through my occupation. However, sadly, C and C++ had been satisfied to let me accomplish that and shoot myself within the foot.

The issue with the problems that I describe above is they result in insects and safety vulnerabilities that hackers can exploit. A memory-safe language like Rust does now not permit those problems to happen. Rust will come across and inform the developer if an overflow happens fairly than silently let it occur.

As an example, Determine 1 beneath displays a easy Rust software that initializes an array of measurement 5 with all zeros. The applying incorporates a easy for loop to print out the array’s values and one index past the array.


fn major() {

    println!(“Hi Global!”);

    println!(“Let’s overflow the buffer!”);


    let array: [u32; 5] = [0;5];


    for index in 0..array.len() + 1 {

        println!(“Index {}: {} “, index, array[index])




Determine 1 – A easy Rust software that makes an attempt to learn knowledge previous the array measurement. 


Once I run this software the usage of shipment run, I am getting the next output:


Determine 2 – Rust catches the try to learn previous the top of the array.


Understand the applying ran simply tremendous, and when it got here to my try at studying handed the buffer, it gave me a runtime error! In C/C++, the applying would were satisfied to learn out that reminiscence location. Alternatively, if I were writing knowledge, it might were happy to overwrite and corrupt the information in that reminiscence location.

The array instance is simply the end of the iceberg. All Rust variables, through default, are immutable except declared another way. There could also be an idea of reminiscence possession. Reminiscence protection is helping builders determine essential insects ahead of they to find their method into our manufacturing code through making us acutely aware of them once we create them!

Explanation why #2 – Dependency control

Should you paintings in C/C++, you recognize there’s no dependency supervisor within the languages. In truth, managing dependencies and making sure that the proper variations of libraries are used could be a small nightmare. Some third-party dependency managers like Conan exist, however the choice of groups that use all these equipment is few and a ways between.

Rust has a integrated bundle supervisor known as Shipment that downloads bundle dependencies, compiles programs, distributes programs, and, if open-sourced, even add them to crates.io. Shipment is helping builders make certain that they’re the usage of the write programs and that even supposing new variations are to be had, they received’t be upgraded till they’re able to improve.

Shipment manages Rust programs dependencies the usage of a toml report. The toml report for a easy software may seem like the next:



identify = “buffer_overflow”

model = “1.0.0”

version = “2022”


# See extra keys and their definitions at https://document.rust-lang.org/shipment/reference/manifest.html



rand = “0.8.3”


Determine 3. A toml report for a easy software.


You’ll see within the above code snippet that the one dependency within the software is a random quantity generator. The model quantity is specified, this means that if I gave you my venture, I don’t want to concern about you having other variations. While you bring together, Shipment gets the precise programs to construct the applying correctly.

Explanation why #3 – Trendy and robust

Rust compiles its code with just about the potency of a C compiler. Should you examine execution instances, the diversities are negligible. Rust can engage with low-level {hardware} successfully and extra safely than C/C++. There are examples of the board start-up programs the place Rust can come across if a pin at the microcontroller isn’t configured appropriately and can warn the developer. We don’t have any form of coverage like that during C/C++; we simply debug and poke round till we notice we didn’t initialize issues correctly.

The language syntax could also be like what a C/C++ program may be expecting, with further enhancements and features. As an example, in C, if I need to initialize an array with 500 parts to all zeros, I want to write the next code:


uint32_t array[500];


for(uint32_t Index = 0; Index


    array[Index] = 0;



I’ve by no means discovered those statements to be chic. It’s at all times felt bizarre having to initialize an array like this. In Rust, I will claim and initialize my array as follows:


let array: [u32; 500] = [0;500];


Succinct. Chic. Concise.


I’ve proven you a trivial instance, however there are such a large amount of examples, and the language is compelling.



Rust supplies a thrilling selection to the standard C/C++ programming languages. With such a lot of techniques having a look to make stronger safety, the usage of a memory-safe language like Rust makes numerous sense. Alternatively, it’s nonetheless essential to notice that there’s recently no Rust language same old. The language continues to conform and alter. Such trade may now not are compatible smartly with each embedded machine or staff. If you have an interest in the usage of Rust, I like to recommend you be told a little extra about it ahead of absolutely committing. A couple of tips come with the next:

  • Learn the loose Rust Embedded Ebook
  • Write processor start-up code to your favourite microcontroller
  • Write a serial driving force
  • Broaden an interrupt-based software

Simplest after you get some hands-on revel in are you able to resolve whether or not Rust suits your wishes.

Leave a Reply