core, alloc, std, and related libraries.
Library Structure
The Rust standard library is organized into several layers:core - The Foundation
The core library is the dependency-free foundation of Rust.Location:
library/core/Features:- No dependencies (not even libc)
- No heap allocation
- No I/O
- Platform-agnostic
- Used in
#![no_std]environments
alloc - Memory Management
The alloc library provides heap allocation primitives.Location:
library/alloc/Features:- Requires a global allocator
- Collections:
Vec,String,Box, etc. - No I/O or OS dependencies
Library Directory Layout
Setting Up for Library Development
Working with Core Library
Core Library Constraints
The core library has special restrictions:Example: Adding to Core
- Safe API
- Unsafe API
- Tests
Working with Alloc Library
Memory Allocation Primitives
The alloc library provides heap-allocated types:Working with Std Library
Platform-Specific Code
The std library contains platform-specific implementations:- Unix
- Windows
- Common Interface
Library Development Workflow
Make your changes
Edit the appropriate library file in
library/core/, library/alloc/, or library/std/.Library Features
Feature Flags in Std
The standard library uses feature flags for conditional compilation:Stability Attributes
All public APIs must have stability attributes:Testing Libraries
Test Organization
- Library Tests
- Doc Tests
- Integration Tests
Running Library Tests
Cross-Platform Development
Platform Abstraction Layers
The std library uses a Platform Abstraction Layer (PAL):Conditional Compilation
Performance Considerations
Use #[inline]
Avoid Allocations
Use iterators and stack allocation when possible
Benchmarking
Size Optimization
Use
optimize_for_size feature for embedded targetsCommon Tasks
Adding a new method to Vec
Adding a new method to Vec
- Edit
library/alloc/src/vec/mod.rs - Add implementation with stability attributes
- Add doc tests with examples
- Add unit tests in
library/alloc/tests/vec.rs - Run
./x.py test library/alloc
Fixing a bug in std::fs
Fixing a bug in std::fs
- Edit
library/std/src/fs.rsor platform-specific file - Add regression test
- Test on multiple platforms
- Run
./x.py test library/std --test-args fs
Optimizing core::slice
Optimizing core::slice
- Edit
library/core/src/slice/mod.rs - Add benchmarks if needed
- Ensure no unsafe code bugs
- Test thoroughly with Miri:
./x.py test library/core --stage 0
Resources
Std Dev Guide
Standard library development guide
Library Team
Chat with the library team on Zulip
API Guidelines
Rust API design guidelines
Libs Tracking
Library issues on GitHub
Next Steps
Compiler Development
Learn about compiler development
Debugging
Debug library issues
Profiling
Profile library performance