liveBook · Manning

No_mangle

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

no_mangle is a Rust attribute that suppresses compiler name mangling for an item’s externally visible symbol.

The no_mangle attribute keeps Rust from changing an item’s symbol name during compilation. Symbols are strings in the compiled binary, and predictable names matter when code must be found by external components. Rust normally mangles names to prevent collisions between libraries; disabling mangling is necessary here because the boot process may otherwise fail. section 11.2.3 section 11.7

Naming a kernel entry point

A small kernel places #[no_mangle] on its _start function and panic handler. The entry point is also declared pub extern "C" so it follows C calling conventions; no_mangle controls the symbol name, while extern "C" controls how arguments are arranged. 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 }

Inspect the complete kernel listing to see no_mangle alongside no_std, no_main, extern "C", and the bare-metal entry point. section 11.2.3

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

How deep it goes
Explains symbol strings, name-mangling collisions, and boot failure risk, but stops short of linker-level details.
Examples
Uses a kernel’s panic handler and _start entry point as concrete no_mangle examples.
What it assumes
Assumes basic Rust attributes and functions, while introducing symbol naming through a bare-metal kernel example.
What you can do after
Apply no_mangle to preserve predictable symbols in a Rust kernel or external-component interface.
How it connects
Connects symbol name mangling with extern C, calling conventions, no_std, and no_main.

Also explained in

Compare how 3 books explain no_mangle →

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.