Core alloc and std boundaries
Quick answer
Embedded firmware may run without an operating system. #![no_std] opts out of automatically linking the standard library, while core supplies fundamental types and operations. alloc supplies heap-backed collections when an allocator is available.
Choose the smallest dependency boundary
A library that uses slices, Option and checked arithmetic can work without a heap. A no_std library may still use alloc explicitly; the final binary must provide the required runtime facilities. A fixed array has bounded capacity, but its storage location depends on how it is created. Keep hardware access outside pure processing code.
#![no_std]pub fn average(values: &[u16]) -> Option<u32> { let n = u32::try_from(values.len()).ok()?; if n == 0 { return None; } let sum = values.iter().try_fold(0u32, |s, &v| s.checked_add(u32::from(v)))?; Some(sum / n)}Manual local Rust/Cargo candidate; not executed. Expected behavior:
cargo check --lib in a library crate; no binary or stdout.Integer division truncates; empty or overflowing input returns None.
fn main() { let mut buffer = [0u8; 4]; let input = [3, 5]; buffer[..input.len()].copy_from_slice(&input); println!("{:?}", &buffer[..input.len()]);}Expected stdout (not executed):
[3, 5]The used length is separate from allocated capacity.
Common mistake
Do not assume no_std means no allocation or that println! is available on bare metal.
Try it yourself
Modify average to reject samples above 4095 and test empty input.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Core alloc and std boundaries — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A no_std library wants Vec. What extra library and runtime support are relevant?
Core alloc and std boundaries — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust #![no_std] pub fn average(values: &[u16]) -> Option<u32> { let n = u32::try_from(values.len()).ok()?; if n == 0 { return None; } let sum = values.iter().try_fold(0u32, |s, &v| s.checked_add(u32::from(v)))?; Some(sum / n) } ``` Which outcome or interpretation best matches this example?
Core alloc and std boundaries — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let mut buffer = [0u8; 4]; let input = [3, 5]; buffer[..input.len()].copy_from_slice(&input); println!("{:?}", &buffer[..input.len()]); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.