Skip to main content
The Rust standard library is composed of multiple layers in the library/ directory, designed to support everything from bare-metal embedded systems to full desktop operating systems.

Library Hierarchy

The standard library follows a layered architecture:
This layered design allows Rust to target platforms ranging from microcontrollers (core only) to full operating systems (std).

Core Crates

core - The Foundation

core is the foundation of the Rust standard library, containing primitives that work everywhere:

[package]
name = "core"
version = "0.0.0"
edition = "2024"
description = "The Rust Core Library"
core is #![no_std] compatible and includes:
  • Primitive types (i32, bool, char, etc.)
  • Core traits (Copy, Clone, Eq, Ord, etc.)
  • Option and Result types
  • Iterators and iterator traits
  • Slice operations
  • String slices (&str)
  • Cell types (Cell, RefCell)
  • Pointers and references
  • SIMD types via portable-simd
The core crate is re-exported by std, so users typically access it through std:: rather than core:: directly.

alloc - Heap Allocation

alloc provides heap-allocated types without requiring a full OS:

Collections

  • Vec<T>: Growable arrays
  • String: Owned strings
  • BTreeMap/BTreeSet: Ordered maps/sets
  • LinkedList: Doubly-linked lists
  • VecDeque: Double-ended queue

Smart Pointers

  • Box<T>: Heap allocation
  • Rc<T>: Reference counting
  • Arc<T>: Atomic reference counting
  • Weak<T>: Weak references
alloc requires:
  • A global allocator
  • Basic OS primitives for memory allocation
alloc does NOT require:
  • File system
  • Networking
  • Threading (except for Arc)
[features]
compiler-builtins-c = []
compiler-builtins-mem = []
optimize_for_size = []
These features are primarily used when building custom sysroots for embedded targets.

std - The Complete Standard Library

std is the full standard library for hosted environments:
[package]
name = "std"
version = "0.0.0"
edition = "2024"
description = "The Rust Standard Library"

[dependencies]
alloc = { path = "../alloc", public = true }
core = { path = "../core", public = true }
panic_unwind = { path = "../panic_unwind", optional = true }
panic_abort = { path = "../panic_abort" }
unwind = { path = "../unwind" }
std_detect = { path = "../std_detect", public = true }
std provides OS-specific functionality:
  • File system operations (std::fs)
  • Networking (std::net)
  • Threading (std::thread)
  • Process management (std::process)
  • Environment variables (std::env)
  • Time and duration (std::time)
  • Synchronization primitives (std::sync)

Supporting Crates

Platform Detection and Features

std_detect

std_detect provides CPU feature detection:
  • SIMD instruction detection
  • Runtime CPU feature detection
  • Architecture-specific features
  • Used by stdarch for portable SIMD

portable-simd

portable-simd provides portable SIMD types:
  • Vector types (Simd<T, N>)
  • Portable across architectures
  • Compile-time and runtime dispatch
  • Integrated with core

Architecture-Specific Support

stdarch contains architecture-specific intrinsics:
  • x86/x86_64: SSE, AVX, AVX2, AVX-512
  • ARM/AArch64: NEON, SVE
  • RISC-V: Vector extensions
  • WebAssembly: SIMD128
  • PowerPC, MIPS, and more
Each architecture has intrinsics for:
  • SIMD operations
  • Atomic operations
  • Special instructions
  • Performance counters

Panic Handling

Two panic runtime options:
panic_unwind implements unwinding panic runtime:
  • Stack unwinding on panic
  • Running destructors during unwind
  • Catching panics with catch_unwind
  • Larger binary size
  • Default for most targets
[features]
panic-unwind = ["dep:panic_unwind"]
unwind provides unwinding abstractions:
  • Interfaces with system unwinding libraries
  • Supports libunwind, LLVM libunwind, or system libunwind

Low-Level Runtime Support

compiler_builtins provides compiler intrinsics:
  • Low-level operations not provided by LLVM
  • Integer division/multiplication on some platforms
  • Floating-point operations
  • Memory operations (memcpy, memset, etc.)
Configured with high codegen units:
[profile.release.package.compiler_builtins]
codegen-units = 10000  # One intrinsic per object file
profiler_builtins provides profiling support:
  • Coverage instrumentation
  • Profile-guided optimization (PGO)
  • Integration with LLVM profiling
Backtrace support integrated into std:
[features]
backtrace = [
  'addr2line/rustc-dep-of-std',
  'object/rustc-dep-of-std',
  'miniz_oxide/rustc-dep-of-std',
]
Dependencies for symbolication:
  • addr2line: DWARF debug info parsing
  • object: Object file parsing
  • miniz_oxide: Compression
  • rustc-demangle: Symbol demangling
rtstartup provides runtime startup code for some platforms.

Procedural Macros

proc_macro is the procedural macro API:
Unlike other standard library crates, proc_macro is special: it runs at compile time in the compiler process, not in the compiled program.
// Proc macros run at compile time
#[derive(Debug)]  // Uses proc_macro infrastructure
struct MyStruct;
Key capabilities:
  • Custom derive macros
  • Attribute macros
  • Function-like macros
  • Token stream manipulation

Testing Infrastructure

test provides the test harness:
#[test]
fn my_test() {
    assert_eq!(2 + 2, 4);
}
  • Test discovery and execution
  • Benchmark support
  • Test filtering
  • Parallel test execution
  • Output capture
  • Custom test frameworks

Workspace Organization

The library workspace is managed by library/Cargo.toml:
[workspace]
resolver = "1"
members = [
  "std",
  "sysroot",
  "coretests",
  "alloctests",
]

exclude = [
  "stdarch",        # Has its own workspace
  "windows_link"    # Platform-specific
]

Rustc Standard Library Workspace Crates

These crates enable the standard library to be used with older rustc versions:

rustc-std-workspace-core

Shim for core crate

rustc-std-workspace-alloc

Shim for alloc crate

rustc-std-workspace-std

Shim for std crate
From library/Cargo.toml:
[patch.crates-io]
rustc-std-workspace-core = { path = 'rustc-std-workspace-core' }
rustc-std-workspace-alloc = { path = 'rustc-std-workspace-alloc' }
rustc-std-workspace-std = { path = 'rustc-std-workspace-std' }

Sysroot Crate

sysroot is a special crate that depends on all standard library crates:
The sysroot crate ensures all standard library components are built and available in the compiler’s sysroot.

Platform-Specific Crates

Windows Support

windows_link handles Windows-specific linking:
[target.'cfg(any(windows, target_os = "cygwin"))'.dependencies.windows-link]
path = "../windows_link"
Features:
[features]
windows_raw_dylib = ["windows-link/windows_raw_dylib"]
Enables raw-dylib for Windows imports (smaller binaries, faster loading).

Platform-Specific Dependencies

[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
libc = { version = "0.2.178", public = true }
libc provides C library bindings for:
  • POSIX APIs
  • System calls
  • C standard library functions

Build Profiles and Optimization

Special build profiles for the standard library:
The dist profile is used for prebuilt standard library artifacts:
[profile.dist]
inherits = "release"
codegen-units = 1
debug = 1  # "limited" debug info
rustflags = [
  "-Cembed-bitcode=yes",           # For LTO
  "-Zunstable-options",
  "-Cforce-frame-pointers=non-leaf", # Better profiling
]
Backtrace dependencies are optimized for size and speed:
[profile.release.package]
addr2line.opt-level = "s"      # Optimize for size
addr2line.debug = 0
gimli.opt-level = "s"
miniz_oxide.opt-level = "s"
rustc-demangle.opt-level = "s"
object.debug = 0

Standard Library Features

The standard library exposes several feature flags:
[features]
backtrace = [...]              # Enable backtrace symbolication
backtrace-trace-only = []      # Backtrace without symbolization
panic-unwind = [...]           # Unwinding panic runtime
compiler-builtins-c = [...]    # C implementation of builtins
compiler-builtins-mem = [...]  # Memory operation builtins
optimize_for_size = [...]      # Optimize for binary size
debug_refcell = [...]          # Additional RefCell debugging
llvm_enzyme = [...]            # LLVM Enzyme support
windows_raw_dylib = [...]      # Raw dylib imports on Windows

Target Support Tiers

The standard library supports multiple tiers of targets:
Full standard library support with guaranteed CI:
  • x86_64-unknown-linux-gnu
  • x86_64-pc-windows-msvc
  • x86_64-apple-darwin
  • aarch64-apple-darwin

Memory Allocator Integration

Different targets use different allocators:

System Allocator

Desktop/server platforms:
  • Linux: glibc malloc
  • Windows: HeapAlloc
  • macOS: malloc/free

dlmalloc

Embedded/WASM platforms:
  • Pure Rust allocator
  • No OS dependencies
  • Suitable for #![no_std] + alloc

Design Principles

Zero-Cost Abstractions

Standard library features compile to efficient code with no runtime overhead

Layered Architecture

core → alloc → std allows targeting diverse platforms

Platform Independence

Core abstractions work consistently across all supported platforms

Safety First

Safe abstractions over unsafe platform APIs

Further Reading

Compiler Architecture

How the compiler uses and compiles the standard library

Bootstrap System

Building the standard library through multi-stage bootstrap

The Rustonomicon

std Documentation

Build docs developers (and LLMs) love