alloc library provides smart pointers and collections for managing heap-allocated values. This library is the middle layer between core (no allocation) and std (full standard library).
The
alloc library normally doesn’t need to be used directly since its contents are re-exported in the std crate. However, #![no_std] crates that need heap allocation will use this crate.Overview
The alloc library provides:- Smart pointers for heap allocation
- Collections for dynamic data structures
- Heap interfaces for memory allocation
- String types for owned text data
Requirements
Smart Pointers
Box - Owned Heap Values
Box<T> - Single ownership
Box<T> - Single ownership
Box is a smart pointer type for heap allocation. There can only be one owner of a Box, and the owner can mutate the contents.- Single ownership (not
Copy, butCloneifT: Clone) - Same size as a pointer
- Efficient for sending between threads
- Ideal for tree-like data structures
- Enables recursive types
Rc - Reference Counting
Rc<T> - Shared ownership (single-threaded)
Rc<T> - Shared ownership (single-threaded)
Arc - Atomic Reference Counting
Arc<T> - Shared ownership (thread-safe)
Arc<T> - Shared ownership (thread-safe)
Collections
Vec<T>
Growable array allocated on the heap
VecDeque<T>
Double-ended queue
LinkedList<T>
Doubly-linked list
BinaryHeap<T>
Priority queue
BTreeMap<K, V>
Ordered map based on B-Tree
BTreeSet<T>
Ordered set based on B-Tree
Vec - Contiguous Growable Array
Vec<T> - The most common collection
Vec<T> - The most common collection
A contiguous growable array type with heap-allocated contents.Performance:
- O(1) indexing
- Amortized O(1) push to end
- O(1) pop from end
- O(n) insert/remove in middle
String - Owned UTF-8 Text
String - Growable UTF-8 text
String - Growable UTF-8 text
A UTF-8 encoded, growable string.Key points:
- Backed by
Vec<u8> - Always valid UTF-8
- Not indexable by position (use iterators)
- Growable and mutable
BTreeMap and BTreeSet
BTreeMap<K, V> and BTreeSet<T> - Ordered collections
BTreeMap<K, V> and BTreeSet<T> - Ordered collections
Ordered map and set implementations based on B-Trees.Characteristics:
- Keys always sorted
- O(log n) operations
- No hash function required
- Predictable iteration order
- Range queries supported
Heap Interfaces
alloc module - Low-level allocation
alloc module - Low-level allocation
The Key types:
alloc module defines the low-level interface to the default global allocator.Layout- Describes memory layout requirementsGlobal- The default allocatorAllocError- Allocation failure error
Common Patterns
Shared Ownership with Mutation
Thread-Safe Shared State
Builder Pattern with Box
String Operations
String manipulation
String manipulation
Format Macro
format! and friends
format! and friends
The Related macros (from std):
format! macro and related formatting infrastructure:println!- Print to stdout with newlineprint!- Print to stdouteprintln!- Print to stderr with newlinewrite!- Write to a buffer
Borrow and ToOwned
Borrowing traits
Borrowing traits
The
borrow module provides traits for borrowing and owned data:Task Support
task module - Async task types
task module - Async task types
Types for working with async tasks:
Stability
The alloc library has been stable since Rust 1.36.0. It provides a stable interface for heap allocation in both std and no_std environments.
Feature Flags
The alloc crate respects several configuration options:no_global_oom_handling- Remove panic on allocation failureno_rc- DisableRctypeno_sync- DisableArcand other sync types
Related Libraries
- core - Dependency-free primitives
- std - Full standard library (re-exports alloc)
- hashbrown - HashMap implementation (used by std)