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


