| name | no-std |
| description | no_std Guide guidance for Fortress Rollback. Use when Writing no_std code, embedded targets, core vs alloc decisions. |
no_std Guide
The std / core / alloc Split
| Layer | Contains | Requires |
|---|
core | Primitives, Option, Result, Iterator, PhantomData | Nothing |
alloc | Vec, String, Box, Rc, Arc, BTreeMap, format! | Global allocator |
std | fs, net, thread, time, HashMap, Mutex, io | Operating system |
Note: HashMap/HashSet are NOT in alloc (need randomness). Use BTreeMap or hashbrown.
Basic Structure
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(all(not(feature = "std"), feature = "alloc"))]
extern crate alloc;
Prelude Pattern
pub use core::cmp::{max, min, Ordering};
pub use core::fmt;
pub use core::mem;
#[cfg(feature = "alloc")]
pub use alloc::{boxed::Box, string::{String, ToString}, vec, vec::Vec};
#[cfg(feature = "std")]
pub use std::collections::{HashMap, HashSet};
#[cfg(all(feature = "alloc", not(feature = "std")))]
pub use hashbrown::{HashMap, HashSet};
Feature Flag Setup
[features]
default = ["std"]
std = ["alloc", "dep:parking_lot"]
alloc = []
[dependencies]
serde = { version = "1", default-features = false, features = ["derive", "alloc"] }
hashbrown = { version = "0.14", optional = true, default-features = false, features = ["alloc"] }
Feature Hygiene
#[cfg(all(feature = "tokio", not(feature = "std")))]
compile_error!("tokio feature requires std");
Common std Replacements
| std | no_std + alloc | no_std (no heap) |
|---|
Vec<T> | alloc::vec::Vec<T> | heapless::Vec<T, N> |
String | alloc::string::String | heapless::String<N> |
HashMap | hashbrown::HashMap | heapless::FnvIndexMap |
Box<T> | alloc::boxed::Box<T> | Stack or arena |
Mutex | spin::Mutex | critical-section |
Time
Accept time from outside -- caller is responsible for timing. This enforces determinism.
Random Numbers
Use seeded PRNG (rand_xoshiro::Xoshiro256PlusPlus). Use BTreeMap or hashbrown for deterministic iteration.
Synchronization
use spin::Mutex;
use alloc::sync::Arc;
Error Handling Without std
std::error::Error is NOT in core (before Rust 1.81).
impl core::fmt::Display for Error { }
#[cfg(feature = "std")]
impl std::error::Error for Error {}
Conditional Compilation
#[cfg(feature = "std")]
mod std_networking;
#[cfg(target_arch = "wasm32")]
fn get_timestamp() -> u64 { }
#[cfg(all(feature = "alloc", not(feature = "std")))]
use hashbrown::HashMap;
Testing
cargo check --no-default-features --features alloc
cargo check --no-default-features
cargo check --target wasm32-unknown-unknown --no-default-features --features alloc
Tests always have std available (#[cfg(test)] runs with std).
Popular no_std Crates
Serialization
serde (default-features=false), postcard, rkyv
Data Structures
heapless, arrayvec, smallvec, hashbrown, indexmap
Utilities
bitflags, bytemuck, byteorder, num-traits, static_assertions
Hashing
ahash, rustc-hash, fnv, sha2, blake3
Random
rand_core, rand_xoshiro, rand_chacha
Embedded
embedded-hal, critical-section, portable-atomic, defmt
Checklist