Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 12

Understanding `no_std` in Embedded Systems

Recommended resources

Hello! Welcome to the third module of our course. We're now shifting gears from the general-purpose, high-level features of Rust, like the powerful iterators we just covered, to the specialized world of embedded systems. Your goal of mastering Embedded Rust begins in earnest here.

This lesson addresses the most fundamental concept in Embedded Rust: the no_std environment. When you write a typical program on your computer, you're using Rust's standard library, std, which provides a wealth of features by assuming it's running on a modern operating system like Windows, macOS, or Linux. But what happens when there is no OS? On a tiny microcontroller, your code is all there is. We'll explore what it means to tell Rust, "Don't assume you have an operating system," and what powerful capabilities you still retain.

Hosted vs. Bare-Metal Environments

Most programming you've done in C++, Python, or even Rust so far has been in a hosted environment. This means your code is "hosted" by an operating system (OS), which manages complex tasks like memory allocation, scheduling threads, accessing files, and networking. The Rust standard library, std, is a brilliant abstraction layer over these OS services.

Embedded programming, especially on microcontrollers, often happens in a bare-metal environment. There is no OS. When the chip powers on, it runs your code and nothing else. There's no file system to read from or a network stack to make requests with, at least not by default.

The Embedded Rust Book provides a great explanation of this distinction.

no_std - The Embedded Rust Book

This resource from the official Embedded Rust Book will introduce you to the two main types of programming environments.

Please read the two sections titled "Hosted Environments" and "Bare Metal Environments". As you read, focus on the key difference: the presence or absence of an operating system and what that implies for the standard library.

This difference is the entire reason no_std exists. We need a way to use Rust's safety and modern features on devices that can't support the full standard library.

This diagram illustrates three common software stacks. "Rich OS" is the hosted environment you're used to. "Bare-Metal" is the `no_std` world we're exploring, where our Rust code sits directly on the hardware. "RTOS" is a middle ground we may explore later.

What is no_std?

no_std is a directive you give to the Rust compiler, using the attribute #![no_std] at the top of your main file. It tells the compiler not to link the standard library (std). This is essential for bare-metal programming because std has dependencies on OS features that simply don't exist on a microcontroller.

So, what do we lose when we give up std?

  • Easy access to heap allocation: Types like Vec and String that can grow in size rely on a global memory allocator, which std provides.
  • Operating system services: Threads, file I/O, networking, command-line arguments, and environment variables are all gone.
  • Standard output: The familiar println! macro won't work out-of-the-box because it needs an OS to handle printing to a console.

But more importantly, what do we keep?


Life without std: The core and alloc Crates

This is the most crucial part: no_std does not mean you lose the core of the Rust language. The Rust standard library is actually built in layers.

no_std: ?no_problem: September 2023 Rust Gamedev Meetup

This segment from the talk "no_std: ?no_problem" provides an excellent overview of the three-part split of Rust's standard library.

Watch the section from the beginning of this explanation where the speaker details the core, alloc, and std crates. Pay attention to what functionality belongs in each crate.

As you just heard, the standard library is composed of three distinct parts:

  1. core: This crate is the absolute foundation of the language. It's always available, even in a no_std environment. It contains the fundamental language primitives, such as Option and Result, numeric types, traits like Iterator (which you just learned about!), and functions for manipulating slices. All the ownership and borrowing rules are enforced, just as they are in a standard program. You lose none of Rust's compile-time safety.
  2. alloc: This optional crate provides types that require a dynamic memory allocator, most notably Box, Vec, and String. You can use this in a no_std project, but you must provide your own memory allocator. For many simple embedded applications, developers choose to avoid dynamic allocation entirely for greater predictability and simplicity.
  3. std: The full standard library. It depends on both core and alloc and adds all the OS-dependent functionality. When you use std, you get everything. When you use no_std, you opt out of this layer.

This layered design is one of Rust's superpowers. It allows the language to scale from massive servers all the way down to the tiniest microcontrollers.

The guide "Intro to Rust on Embedded" has a concise table summarizing this.

Intro to Rust on Embedded: Your First no_std Project with Code Examples – Hubble Network Community

This guide provides a very direct explanation of what no_std means and what it provides.

Read the section "What Does no_std Mean in Rust?". The table here is an excellent reference.

Here is a similar table from the Embedded Rust Book, which provides a bit more detail.

This table clearly shows what features are available in `no_std` versus `std` environments. Note the asterisks: heap memory and collections are available in `no_std` only if you explicitly add and configure an allocator.

The Purpose: Control and Portability

So, why go through this trouble? The purpose of using a no_std environment is to gain precise control over your program in a resource-constrained environment.

  • Minimal Binary Size: Because you only include what you explicitly need, no_std binaries are extremely small, which is critical when a microcontroller might only have a few kilobytes of flash memory.
  • Predictability: Without an OS scheduler or a complex global allocator, the behavior of your program becomes much more predictable, which is essential for real-time systems.
  • Portability: A no_std library that only depends on core can run on literally any hardware target that a Rust compiler supports, from a wristwatch to a server. It is completely platform-agnostic.

This leads to a powerful software design principle often called "sans-io" (without I/O). By keeping your core logic in no_std compatible code, you separate the "what to do" (the algorithm) from the "how to do it on this specific platform" (the I/O, the hardware interaction). This makes your logic immensely more testable and reusable.


A Glimpse into a no_std Program

A no_std program looks a bit different from a standard one. Since there's no OS to set things up, we have to provide a few more things ourselves.

rust runs on EVERYTHING (no operating system, just Rust)

This short video gives a great practical demonstration of converting a default Rust project into a no_std one. This is a perfect preview of what we'll do in the next lesson.

Watch from the code modification. Observe the addition of #![no_std] and #![no_main]. Notice that we must define our own entry point (here called start) and also provide a panic_handler, because the default handlers are part of std.

Don't worry about memorizing these details now. The key takeaway is that in a bare-metal environment, we take full control of the program's lifecycle, from the very first instruction it executes.


Conclusion

You've just taken your first and most important step into the world of Embedded Rust. Understanding the no_std environment is the key that unlocks Rust's ability to run on microcontrollers and other resource-constrained devices.

Here are the key points from this lesson:

  • Standard Rust programs run in a hosted environment and use the std library, which relies on an operating system.
  • Embedded systems are often bare-metal environments with no OS, requiring us to use #![no_std].
  • Using no_std does not remove Rust's core features. The core crate provides fundamental types and traits, including Option, Result, and Iterator.
  • The purpose of no_std is to create small, predictable, and portable binaries for devices where we have direct control over the hardware.

In our next lesson, we will move from theory to practice. You will set up your very first no_std project for an ARM Cortex-M microcontroller and see these concepts come to life in code.