5 Tips for Learning Rust for Embedded Systems
Could Rust become the alternative option to C/C++ or the dominant embedded programming language? Prepare yourself just in case.
February 3, 2023
Deciding on the programming language for your next embedded product may not be as simple as just choosing C. While C has been the industry's go-to workhorse for the past 50 years, its features and capabilities dramatically lag modern languages like C++ and Rust. As a result, I’ve witnessed many teams transitioning from C to C++; however, could there be a place for Rust?
Whether you are zealous about adopting Rust for embedded systems or simply curious about the language, there are five tips you can follow to help you get up to speed and understand whether Rust is suitable for your projects.
Tip #1 – Read the Rust Books
The Rust community has been fantastic about creating learning materials for the software community and making them readily available. However, if you don’t know anything about Rust, there are several resources that I would recommend you check out.
The Rust Programming Language. The introductory book introduces developers to the Rust toolchain and essential concepts such as ownership, structured data, pattern matching, language syntax, application packaging, and more.
Discovery. A microcontroller introductory book that uses Rust to introduce readers to Rust. The book is excellent for students and beginners but may not be a great read for embedded software developers with experience. However, the book does choose a “choose your own adventure” with hands-on learning.
The Embedded Rust Book. Aimed at experienced embedded developers to give them the language skills they need to write embedded code with Rust, this book walks through getting started with Rust, the toolchain, simulation, and on-target deployment using STM32 parts. If you have experience, this is the place to start!
I would also recommend that you check out the Embedded Rust Documentation. The community regularly updates these resources and is an excellent source of knowledge.
Tip #2 – Get Hands-on With a Project
The best way to learn a programming language is to get hands-on and make something real. Therefore, if you are serious about trying out Rust for embedded, you need to consider a project you are interested in and try to build. Of course, I’m not saying you must invest all your weekends and create a product you will take to market. However, a simple hobby project can do.
For years, I’ve used a weather station as my technological tryout. If you go back through my courses, blogs, and content, you’ll discover that I’ve built weather stations:
In Python on Raspberry Pis
In C on STM32s
In MicroPython on Netduinos
In C++ on STM32L4s
In Rust for an STM32F3
I’m not overly passionate about the weather; I just have found that once I built a weather station, I understood the concepts and hardware. Making it again in different languages on slightly different hardware helps me to:
Learn the new syntax
Learn language concepts
Understand how to map memory on the microcontroller
Write drivers and interfaces
Interface with external devices
Write reusable code
Find something simple that interests you, and then learn Rust by building it!
Tip #3 – Attend Design News's Rust CEC Course
Formal training can help you learn a language much quicker. You can learn from an instructor of the best practices, what to do, what not to do, and so forth. Design News has its Digikey Continuing Education Center (CEC), which has a ton of great courses. One upcoming course is entitled Rust for Embedded, and the speaker will be yours truly. The course will run from February 28 – March 3rd, 2023. The recordings will be available in the archive afterward, but you might want to consider attending live so that you can ask questions.
Tip #4 – Leverage CoPilot to Accelerate Learning
An exciting environment to learn Rust is Visual Studio Code (VSC). VSC has a GitHub Copilot extension that can suggest and help autocomplete code. If you download the Rust extension, it also provides an excellent resource for you to see the Rust code.
The following is an example of a simple Rust application that I wrote to print “Hello World,” read in 10 characters, and then print them to the terminal:
// Rust main function
fn main() {
println!("Hello, world!");
// Rust array with 10 elements initialized to 0
let mut a = [0; 10];
// Read characters from keyboard in a
for i in 0..10 {
a[i] = read!();
}
// Print array
for i in 0..10 {
print!("{} ", a[i]);
}
}
Let me be clear: the only code I wrote was the comments. Based on my comments, Copilot generated or recommended the code that came after. It’s a simple, trivial example, but I think you can see the power of such a technique!
Tip #5 – Use ChatGPT to Improve Your Code
The code that GitHub Copilot generated was pretty cool. It helps you to write working code. However, was that code it generated the best to use? Did it fit with Rust's idiomatic syntax? Would an experienced Rust developer cringe at the code we just wrote? Possibly. So how might we improve it if we don’t understand the language?
Another way to improve your code is to ask ChatGPT to improve it or to use the open AI auto code completion playground. When I asked the AI model, “Can you improve the following Rust code to be more Rust idiomatic?” it responded with the following:
// Improved version
fn main() {
println!("Hello, world!");
let mut a: [i32; 10] = [0; 10];
for i in 0..10 {
a[i] = read_input().unwrap_or(0);
}
for i in 0..10 {
print!("{} ", a[i]);
}
}
fn read_input() -> Result {
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
input.trim().parse()
}
We can see that this code now reads in a string and parses and will return an error if one is generated. If you were new to Rust, you’d discover some new syntax and libraries to help make life easier.
Conclusions
Rust is an exciting language. It may one day become the dominant embedded programming language or become an alternative option to C/C++. There are many ways to learn Rust between books, hands-on projects, courses, and even leveraging AI models to guide you. As a system language, Rust appears to have an eye on embedded development. Whether it will come to dominate the industry is yet to be seen. Either way, I believe you’ll find that Rust is an interesting language worth learning. Whether you adopt it as your go-to language, only time will tell.
About the Author
You May Also Like