liveBook · Manning

No_std program

As explained in Rust in Action: Systems programming concepts and techniques

no_std is a Rust crate attribute that prevents automatic linkage of the Rust standard library.

A `no_std` program uses `#![no_std]` at the top of its crate to exclude Rust’s standard library. This is useful for a kernel or embedded program that cannot rely on an operating system to provide dynamic memory allocation. Standard-library types such as `Vec<T>` are then unavailable. section 11.2.3 section 11.7

Removing the standard library also means supplying functionality an ordinary Rust program receives automatically. In particular, the program needs a panic handler. A minimal kernel can abort on panic; an infinite loop is another option, but it keeps a CPU core busy until shutdown. section 11.2.4

A kernel that writes to the screen

Place `#![no_std]` and `#![no_main]` above a panic handler and an `_start` function. In this small kernel, `_start` writes a byte to a framebuffer address and then loops forever: there is no operating system to return to. section 11.2.3

Listing 11.4 Creating an OS kernel that paints a block of color
 1 #![no_std]                              #1
 2 #![no_main]                             #1
 3 #![feature(core_intrinsics)]            #2
 4   
 5 use core::intrinsics;                   #2
 6 use core::panic::PanicInfo;             #3
 7  
 8 #[panic_handler]
 9 #[no_mangle]
10 pub fn panic(_info: &PanicInfo) -> ! {
11   intrinsics::abort();                  #4
12 }
13  
14 #[no_mangle]
15 pub extern "C" fn _start() -> ! {
16   let framebuffer = 0xb8000 as *mut u8;
17  
18   unsafe {
19     framebuffer
20       .offset(1)                        #5
21       .write_volatile(0x30);            #6
22   }
23  
24   loop {}
25 }

The complete listing puts the crate attributes, panic handler, entry point, and framebuffer write together. section 11.2.3

As the kernel grows, other functionality normally supplied through libraries may also need an explicit implementation. The minimal exception-handling personality routine is one example. section 11.4.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains the attribute’s effect and works through a minimal kernel, including manual panic handling, without detailing standard-library internals.
Examples
Shows a kernel that writes to a framebuffer, plus aborting and infinite-loop panic handlers.
What it assumes
Assumes familiarity with ordinary Rust projects while explaining why a program without an operating system cannot rely on their usual facilities.
What you can do after
Build it from scratch as a minimal Rust kernel with `#![no_std]`, a custom entry point, and a panic handler.
How it connects
Connects no_std to dynamic memory allocation, panic handlers, entry points, compiler intrinsics, and language items.

Also explained in

Compare how 2 books explain no_std program →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.