What no_std Means and What You Lose Without the Standard Library
no_std removes Rust's standard library and keeps core. See what disappears (heap, threads, files, println), what stays, and how alloc fits back in.

A microcontroller with 64 KiB of flash and 20 KiB of RAM cannot host a Rust program that assumes an operating system, and the first compile error most people meet in embedded Rust says exactly that: can't find crate for std. The attribute #![no_std] is how a crate tells the compiler not to link the standard library, and it is the line that separates firmware from a desktop program. This post explains what the attribute removes, what it leaves, and how to get a heap back when you want one, using Rust 1.85 and the thumbv7em-none-eabihf target as the running example. It is the first rung of the complete guide to embedded Rust on microcontrollers, which covers the whole ladder.
Three crates, not one
The Rust standard library is a facade over three layers, documented in the Rust standard library docs and the Embedded Rust book:
| Crate | Needs | Provides |
|---|---|---|
core | nothing | primitive types, Option, Result, iterators, slices, str, fmt traits, atomics, ptr, mem, cell |
alloc | a global allocator | Box, Vec, String, Rc, Arc, BTreeMap, format! |
std | an operating system | threads, files, sockets, HashMap, println!, env, process, Mutex |
#![no_std] drops the third row. Your crate still gets core automatically. alloc is available only if you ask for it and supply an allocator. That is the whole feature: it is a statement about what your code may assume about the platform.
The target matters as much as the attribute. A target such as thumbv7em-none-eabihf has none as its operating system, which means rustup ships core and alloc for it but not std. So even a crate without the attribute fails to build for that target.
What you lose, and what each loss means
Printing. println! writes to standard output, and there is no standard output on a chip. You replace it with a UART, a semihosting call, or a logging crate such as defmt. The formatting machinery survives, because core::fmt is in core: you can implement core::fmt::Write for a UART type and use write! on it.
The heap. Vec, String and Box need an allocator. Without one, you use fixed-size arrays, slices, and crates such as heapless that provide capacity-bounded collections on the stack or in statics. This is not only a restriction: a firmware with no allocator cannot fail an allocation, which removes a whole class of field failure.
Threads and locks. There is no std::thread and no std::sync::Mutex. Concurrency is interrupts and, optionally, an executor or RTOS. Atomics exist in core::sync::atomic, but how many your chip supports depends on the architecture: a Cortex-M0 has no compare-and-swap instruction, so AtomicUsize::compare_exchange is unavailable there.
Files, time and the clock. std::fs, std::time::Instant and SystemTime are gone. Time comes from a timer peripheral.
**main.** The standard runtime that calls main after setting up the process does not exist, so you need #![no_main] and an entry point from a runtime crate such as cortex-m-rt.
Unwinding. Panics cannot unwind to a catch. You write a panic handler, or you pull in one of the crates that define it.
The smallest complete no_std program
Here is a library-style function that runs unchanged on a laptop and a microcontroller, because it uses only core:
#![no_std]
/// Mean of a window of raw ADC readings, or None for an empty window.
pub fn mean(samples: &[u16]) -> Option<u16> {
if samples.is_empty() {
return None;
}
let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum();
Some((sum / samples.len() as u32) as u16)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn averages_a_window() {
assert_eq!(mean(&[100, 200, 300]), Some(200));
assert_eq!(mean(&[]), None);
}
}cargo test on the host prints:
running 1 test
test tests::averages_a_window ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered outTests link std even though the library does not, because the test harness is a host program. That is a useful property: logic that is no_std can be tested on the host with ordinary cargo test, and only the hardware-touching parts need a board.
The smallest firmware binary
A binary needs two more things the library did not: no main and a panic handler.
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use panic_halt as _;
#[entry]
fn main() -> ! {
let mut counter: u32 = 0;
loop {
counter = counter.wrapping_add(1);
// Keep the optimizer from deleting the loop.
core::hint::black_box(counter);
}
}#[entry] from cortex-m-rt marks the function the reset handler jumps to after it initializes RAM, and the -> ! return type says it never returns. use panic_halt as _; links the panic-halt crate only for its side effect: it defines the #[panic_handler] that loops forever. Remove that line and the linker reports:
error: `#[panic_handler]` function required, but not foundThat message is the signature of a no_std binary missing its panic story. The fix is any crate that provides one (panic-halt, panic-probe, panic-semihosting), or your own handler, which a later post in this series builds.
Getting a heap back
If a crate you depend on needs Vec or String, you can opt in to alloc. You supply a global allocator and declare the crate:
#![no_std]
#![no_main]
extern crate alloc;
use alloc::vec::Vec;
use core::mem::MaybeUninit;
use cortex_m_rt::entry;
use embedded_alloc::LlffHeap as Heap;
use panic_halt as _;
#[global_allocator]
static HEAP: Heap = Heap::empty();
#[entry]
fn main() -> ! {
{
const HEAP_SIZE: usize = 8 * 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(&raw mut HEAP_MEM as usize, HEAP_SIZE) }
}
let mut readings: Vec<u16> = Vec::new();
readings.push(512);
readings.push(530);
loop {
core::hint::black_box(&readings);
}
}The embedded-alloc crate supplies the allocator type, and the code gives it an 8 KiB region of static memory. You choose the size, so the heap's worst-case footprint is a number in your source. Two warnings come with this. A Vec::push can fail by aborting when the heap is exhausted, and the allocator's behavior on that path is the allocator's, so budget the heap or avoid growth after startup. And fragmentation is real on a system that runs for months, which is the reason many production firmware projects stay allocator-free and use heapless containers instead.
Features that quietly pull in std
A no_std crate can still depend on a crate that enables std through a Cargo feature. Many crates ship with a default feature named std that you must switch off:
[dependencies]
serde = { version = "1", default-features = false, features = ["derive"] }Without default-features = false the build fails for a bare-metal target with a long list of unresolved std items. The fix is almost always the same: look in the crate's Cargo.toml for a std feature and disable the default set. Features are additive across the dependency graph, so one transitive crate enabling std for a shared dependency breaks the whole build, and cargo tree -e features shows who asked for it.
When no_std is the wrong tool
If your target runs Linux, even a tiny one, std works and gives you threads, sockets and files. Embedded Linux boards such as a Raspberry Pi or a BeagleBone are std targets. no_std is for bare metal and RTOS-style firmware, and for libraries that want to run in both worlds. A few microcontroller-class targets (ESP32 through ESP-IDF) also have a std port, built on a C runtime that the vendor provides, at the cost of a larger image.


