Rust no_std skill for embedded and bare-metal development. Use when writing
Guide agents through #![no_std] Rust development: what core and alloc provide vs std, implementing custom global allocators, panic handler selection for embedded targets, and strategies for testing no_std crates on the host machine.
// src/lib.rs
#![no_std]
// core is always available (no OS needed)
use core::fmt;
use core::mem;
use core::slice;
// alloc: heap collections — requires a global allocator
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::{vec::Vec, string::String, boxed::Box, format};
pub fn add(a: u32, b: u32) -> u32 {
a + b
}
# Cargo.toml
[features]
default = []
alloc = [] # opt-in to heap allocation
[dependencies]
# no_std-compatible dependencies only
| Crate | Requires OS | Requires heap | Provides |
|---|---|---|---|
core | No | No | Primitives, traits, iter, fmt, mem, ptr, slice, option, result |
alloc | No | Yes (allocator) | Vec, String, Box, Arc, Rc, HashMap (requires global allocator) |
std | Yes | Yes | All of core + alloc + OS APIs (threads, files, sockets, env) |
std re-exports everything in core and alloc, so use std::fmt and use core::fmt are equivalent when std is available.
What's available in core only (no heap, no OS):
// These work in no_std: