liveBook · Manning

No_std program

As explained in Embedded Software with Rust

no_std is a Rust crate configuration that excludes the standard library.

A no_std program uses the crate-level attribute #![no_std] to omit Rust’s standard library. This is useful for bare-metal firmware, where an operating system and its services are unavailable. The core crate still provides fundamental types, traits, iterators, and error-handling primitives such as Option and Result. section 1.3.1 section 3.4.1

Without std, there are no built-in file, socket, or thread APIs, and heap allocation is not available by default. Prefer stack or static storage and fixed-capacity data when practical. If a heap is needed, declare alloc and provide a global allocator; allocation failure remains a real concern on a small microcontroller. section 1.3.1 section 7.1.2

#![no_std]

fn my_function() {
    // Will not compile in pure `no_std` (no allocator available):
    // let v: Vec<u8> = Vec::new();

    // Use fixed-capacity data instead:
    let mut buf: [u8; 32] = [0; 32];
    let mut len = 0;
    if len < buf.len() { buf[len] = 0xAB; len += 1; }
}

A fixed-size byte array replaces a heap-allocated Vec and tracks the used length explicitly. section 1.3.1

For a bare-metal executable without a runtime crate, #![no_main] disables the normal entry point separately from #![no_std]. The program must provide an entry point and a non-returning panic handler. A linker script and startup code must also arrange the target’s memory layout and initialization; the attributes alone do not make firmware ready to run. section 3.4.2 section 4.0

#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[unsafe(no_mangle)]
pub unsafe extern "C" fn _start() -> ! {
    // Place initialization steps here (memory setup, peripherals, etc.)

    main();

    loop {}
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

fn main() -> ! {
    loop {
        // Application logic here
    }
}

This minimal structure separates a custom entry point, a panic handler, and application logic. Its initialization and target-specific startup still need to be supplied. section 4.4.6

Instead of implementing all startup support manually, firmware can use a runtime crate such as cortex-m-rt or riscv-rt. A no_std crate can also be built as a static library for C; the final linked no_std artifact needs panic behavior even when the library has no main function. section 2.1.1 section 8.3.2

How Embedded Software with Rust covers it

How deep it goes
Explains crate attributes, panic handling, storage choices, and minimal entry-point structure, with startup and memory-layout responsibilities.
Examples
Shows a fixed-capacity byte buffer, a minimal bare-metal entry point, and a Rust static library callable from C.
What it assumes
Assumes embedded programming experience while explaining no_std configuration and its runtime responsibilities.
What you can do after
Builds and runs minimal no_std programs from scratch in an emulator and on hardware.
How it connects
Connects no_std to core, alloc, global allocators, linker scripts, runtime crates, and C static libraries.

Also explained in

Compare how 3 books explain no_std program →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.