Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Concepts

no_std programming in Rust

Reference for no_std Rust: the core, alloc and std layers, #![no_std], panic handlers, global allocators, what is missing without std and how to write portable libraries.

The Rust standard library is built in three layers. core has no dependency on an operating system or heap: primitive types, Option, Result, iterators, fmt, mem, ptr, atomics, slice and str. alloc adds heap types (Box, Vec, String, Rc, Arc, BTreeMap, format!) and needs a global allocator. std re-exports both and adds the OS-backed parts: files, networking, threads, HashMap (which needs random seeds), time and the process environment.

Adding #![no_std] at the crate root drops std from the prelude and uses only core. It is how firmware, kernels, bootloaders and WebAssembly modules are written, and also how many libraries stay portable.

Rust
#![no_std]
extern crate alloc;           // opt in to Vec, String, Box
use alloc::vec::Vec;

Try it

Rust Seeing the layers: the same types from core, alloc and std

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust A no_std-friendly library function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Fixed-capacity buffer without a heap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

What a no_std binary must provide

Piece Why
#![no_std] and usually #![no_main] no runtime to call main; you define the entry point (for example with an embedded runtime crate)
A panic handler #[panic_handler] fn(&PanicInfo) -> ! core needs to know what a panic does (loop, reset, log)
A global allocator #[global_allocator] only when you use alloc
A target for the hardware for example thumbv7em-none-eabihf, riscv32imac-unknown-none-elf, wasm32-unknown-unknown
A linker script / startup code usually supplied by crates such as cortex-m-rt

Our runner builds hosted programs with std, so the bare-metal pieces are shown read-only; the code examples above run because std re-exports core and alloc.

Usage notes

  • Libraries: the common pattern is #![cfg_attr(not(feature = "std"), no_std)] with an optional std feature, so one crate serves both worlds.
  • Not in core: HashMap/HashSet (use BTreeMap from alloc, or the hashbrown crate), std::io, std::fs, std::thread, std::time::SystemTime, std::error::Error (it moved to core::error::Error, stable since Rust 1.81).
  • Fallible allocation: without std, Vec::try_reserve returns an error instead of aborting, which matters in constrained systems.
  • No panics in firmware: prefer Result, checked_* and get; configure panic = "abort" to shrink code.
  • Floating point math functions like sin and powf live in std; core has the operators and basic methods, and crates like libm fill the gap.
  • Testing: unit tests of no_std code run on the host with std as normal, using #[cfg(test)].
  • Edition and tooling: rustup target add <triple> installs a target's core library; cargo build --target ... builds for it. These are read-only here.