A `no_std` program uses the crate attribute `#![no_std]` to prevent automatic linkage of Rust’s standard library. This is useful for a kernel or an embedded program that cannot rely on an operating system to provide services such as dynamic memory allocation. Types such as `Vec<T>` are not automatically available, so the kernel example avoids dynamically allocating memory. section 11.2.3 section 11.7
A small kernel can still use `core`. Its entry point writes a byte to a framebuffer address and then loops rather than returning: there is no other program to return to. The listing also uses `#![no_main]`, a C calling convention, and an unmangled entry-point name for booting; these are separate choices from `#![no_std]`. 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 kernel listing places `#![no_std]` at the top, imports from `core`, defines a panic handler, and writes to the framebuffer. section 11.2.3
Removing the standard library leaves some work for the program. A panic handler must be supplied: the example calls `intrinsics::abort()`. An alternative handler loops forever, but keeps the CPU core busy until shutdown. The intrinsic used in the example requires an unstable feature and a nightly compiler. section 11.2.4 section 11.2.3
Building the kernel also requires configuration. The Cargo configuration selects a custom target and builds `core` and `compiler_builtins`. Minimal exception handling can require supplying additional functionality normally associated with the standard library. section 11.2.3 section 11.4.1
Listing 11.3 Extra build-time configuration for cargo
[build] target = "fledge.json" [unstable] build-std = ["core", "compiler_builtins"] build-std-features = ["compiler-builtins-mem"] [target.'cfg(target_os = "none")'] runner = "bootimage runner"
The build configuration selects the kernel target and specifies the components to build. section 11.2.3

