liveBook · Manning

Core library

As explained in Embedded Software with Rust

Rust's core crate provides fundamental types, traits, and APIs without depending on an operating system or heap.

Rust’s core crate supplies fundamental types, traits, and APIs without requiring an operating system or heap. In bare-metal firmware, a no_std build omits std and uses core as its baseline, alongside a target-appropriate startup or runtime crate. section 1.3 section 7.1.2

Core includes primitive types, references, slices and arrays; traits such as Iterator, Debug and Clone; Option and Result; and Cell and RefCell. Atomic types are available where the target supports them. section 1.3.1 section 7.1.2

Unlike std, core does not provide files, sockets, threads or a default heap. For firmware that does not need dynamic allocation, prefer stack or static storage and fixed-capacity data structures. 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-allocating Vec in a no_std function. The separate length tracks how much of the buffer is used. section 1.3.1

When heap-backed types such as Vec, String or Box are genuinely needed, a no_std program can add alloc. Declare extern crate alloc at the crate root and provide a global allocator; without an allocator, the program will not link. Allocation can also fail on a memory-constrained microcontroller. section 7.1.2

How Embedded Software with Rust covers it

How deep it goes
Distinguishes core APIs from alloc and describes allocator setup, while deferring allocator options and failure handling.
Examples
Uses a fixed-size byte array with a tracked length to replace a heap-allocating Vec.
What it assumes
Assumes familiarity with embedded programming while introducing Rust for firmware.
What you can do after
Apply core to a no_std buffer using fixed-capacity storage instead of a Vec.
How it connects
Connects core to no_std, alloc, static storage and fixed-capacity structures.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.