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:- Key Features
- No Dependencies
- Features
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 arraysString: Owned stringsBTreeMap/BTreeSet: Ordered maps/setsLinkedList: Doubly-linked listsVecDeque: Double-ended queue
Smart Pointers
Box<T>: Heap allocationRc<T>: Reference countingArc<T>: Atomic reference countingWeak<T>: Weak references
- A global allocator
- Basic OS primitives for memory allocation
- File system
- Networking
- Threading (except for
Arc)
alloc Features
alloc Features
std - The Complete Standard Library
std is the full standard library for hosted environments:- Platform Support
- Platform Dependencies
- Re-exports
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:Supported Architectures
Supported Architectures
- x86/x86_64: SSE, AVX, AVX2, AVX-512
- ARM/AArch64: NEON, SVE
- RISC-V: Vector extensions
- WebAssembly: SIMD128
- PowerPC, MIPS, and more
- SIMD operations
- Atomic operations
- Special instructions
- Performance counters
Panic Handling
Two panic runtime options:- panic_unwind
- panic_abort
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
- Interfaces with system unwinding libraries
- Supports libunwind, LLVM libunwind, or system libunwind
Low-Level Runtime Support
compiler_builtins
compiler_builtins
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.)
profiler_builtins
profiler_builtins
profiler_builtins provides profiling support:
- Coverage instrumentation
- Profile-guided optimization (PGO)
- Integration with LLVM profiling
backtrace
backtrace
Backtrace support integrated into std:Dependencies for symbolication:
addr2line: DWARF debug info parsingobject: Object file parsingminiz_oxide: Compressionrustc-demangle: Symbol demangling
rtstartup
rtstartup
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.
- Custom derive macros
- Attribute macros
- Function-like macros
- Token stream manipulation
Testing Infrastructure
test provides the test harness:- Test Features
- Internal Tests
- Test discovery and execution
- Benchmark support
- Test filtering
- Parallel test execution
- Output capture
- Custom test frameworks
Workspace Organization
The library workspace is managed bylibrary/Cargo.toml:
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
library/Cargo.toml:
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:Platform-Specific Dependencies
- Unix-like
- WASM/Embedded
- WASI
- Exotic Targets
- POSIX APIs
- System calls
- C standard library functions
Build Profiles and Optimization
Special build profiles for the standard library:Distribution Profile
Distribution Profile
The
dist profile is used for prebuilt standard library artifacts:Dependency Optimization
Dependency Optimization
Backtrace dependencies are optimized for size and speed:
Standard Library Features
The standard library exposes several feature flags:Target Support Tiers
The standard library supports multiple tiers of targets:- Tier 1
- Tier 2
- Tier 3
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
Advanced topics: https://doc.rust-lang.org/nomicon/
std Documentation
API documentation: https://doc.rust-lang.org/std/