array_list
array_list is an ordered collection backed by fixed-capacity chunks. It gives
you familiar sequence operations, iterators, and cursors while avoiding one
allocation per element.
Installation
Add array_list to your Cargo.toml:
or edit your Cargo.toml manually by adding:
[]
= "0.5"
This crate requires Rust 1.87 or newer.
Quick Start
use ArrayList;
let mut list: = from;
list.insert;
list.push_front;
list.push_back;
assert_eq!;
for value in &mut list
assert_eq!;
Features
- Ordered sequence with index-based access.
- Chunked storage with less per-element pointer overhead than a traditional linked list.
- Shared, mutable, and owning iterators.
- Cursor and mutable cursor APIs for moving around the list and editing near the cursor.
#![no_std]support withalloc.
When To Use It
array_list is useful when you want an ordered sequence that supports indexed
access, stable iteration order, and cursor-local edits without allocating one
node per element.
It is not a drop-in replacement for Vec. Index lookup may need to scan chunks
when the list is not densely packed, and insertions/removals inside a chunk can
move elements within that chunk. It is also not a drop-in replacement for
LinkedList: elements inside each chunk are stored contiguously, but operations
can still move values inside a chunk or split chunks when capacity is reached.
Use Vec when you primarily push/pop at the back and need dense contiguous
storage. Use ArrayList when cursor-local edits and chunked growth are a better
fit than a single contiguous allocation. Use LinkedList only when its exact
node-based semantics are required.
API Overview
- Build lists with
ArrayList::new,ArrayList::from,collect,extend, orappend. - Edit at the ends with
push_front,push_back,pop_front, andpop_back. - Edit by index with
insert,remove,get, andget_mut. - Traverse with
iter,iter_mut,into_iter, or theIntoIteratorimplementations forArrayList,&ArrayList, and&mut ArrayList. - Navigate and edit near a position with
CursorandCursorMut. - Inspect storage with
capacity,spare_capacity, andshrink.
Storage Model
ArrayList<T, N> stores elements in chunks that can each hold up to N
elements. The logical order is independent from the internal chunk boundaries:
iteration and indexing always see one continuous sequence.
Middle insertions and removals may leave spare capacity inside chunks. After a
middle removal that leaves the edited chunk non-empty, or after a middle
insertion that splits a full chunk, the edited chunk is locally refilled up to
half capacity by taking elements from immediate sibling chunks when those
siblings contain more than half capacity. Siblings at or below half capacity are
left untouched, so edit-heavy workloads can still become fragmented. Use
spare_capacity to inspect unused chunk slots and shrink to compact elements
toward the front while preserving logical order.
This is a local minimum-occupancy heuristic, not a global invariant. The minimum
is N / 2 using integer division, so odd capacities round down. Refill only
touches the edited chunk and its immediate siblings, and it prefers bounded
local movement over globally dense storage. Inserting into a full middle chunk
splits that chunk locally. Front/back pushes and pops keep their direct boundary
behavior, so edge chunks may be less than half full.
Chunk Capacity
The second type parameter is the maximum number of elements per chunk:
use ArrayList;
let list: = new;
Capacity must be at least 4. Smaller chunk sizes are rejected at compile time by constructors and operations because this data structure is designed around multi-element chunks and a local half-full occupancy heuristic.
Choose N based on your workload. Chunk size has a large effect on performance:
small capacities reduce the maximum amount moved inside one chunk, but they also
create many more chunks, more boundary crossings, more allocations, and more
metadata to scan. Larger capacities improve locality and reduce chunk count,
which is especially important for cursor-local edits, but middle insertions and
removals can move more elements within the edited chunk and its immediate
siblings.
Very small capacities are mostly useful for stress-testing chunk boundaries. For general use, prefer capacities large enough to amortize chunk overhead. Values in the 32-64 range are usually more representative than the minimum supported size; mutation-heavy workloads often benefit from the larger end of that range.
Keeping edited chunks at least half full where possible reduces the chance of
many tiny chunks after repeated mutations. The tradeoff is that the list may
contain more chunks than a fully compacted layout, so locating an indexed element
can require scanning more chunk metadata until shrink is called.
Performance Notes
The exact cost depends on chunk occupancy and the chosen chunk capacity:
lenandis_emptyare constant time.- End pushes and pops usually touch only one edge chunk.
get,get_mut,insert, andremovelocate elements by scanning chunk metadata unless the list is densely packed.- Middle insertion/removal may move elements within the edited chunk and its immediate siblings.
- Iteration is in logical order and does not expose chunk boundaries.
Call shrink after edit-heavy phases if a compact front-filled layout is more
important than preserving spare chunk slots for future edits.
no_std
array_list is #![no_std] and uses alloc, so it can be used in environments
that provide an allocator but not the full standard library:
extern crate alloc;
use ArrayList;
let list: = from;
assert_eq!;
Example
use ArrayList;
Cursors
Cursor and CursorMut point either at an element or at the ghost position
after the back of the list. Moving forward from the ghost wraps to the front;
moving backward from the front moves to the ghost.
Mutable cursor edits keep the cursor on the same logical element when possible.
For example, insert_before inserts before the current element and leaves the
cursor on that original element; remove_current removes the current element and
moves the cursor to the next element, or to the ghost position if there is no
next element.
use ArrayList;
let mut list: = from;
let mut cursor = list.cursor_front_mut;
cursor.insert_after;
cursor.move_next;
cursor.insert_after;
assert_eq!;
assert_eq!;
Testing
Run the default test suite:
Run the focused Miri suite:
When cfg(miri) is active, the regular unit and property tests are compiled
out so Miri only runs the targeted ownership, cursor, and iterator checks.
Build the benchmark suite:
Run the Criterion benchmarks:
Check formatting, lints, and public API documentation:
Contributing
Contributions are welcome. Whether it is improving documentation, fixing bugs, or suggesting new features, feel free to open an issue or submit a pull request.
When contributing, please ensure:
- Code is formatted with
cargo fmt. - Tests are added or updated as necessary.
- Safety is maintained for any
unsafecode introduced.
By contributing, you agree that your contributions will be licensed under the terms of the MIT license.
License
This crate is licensed under the MIT License. You are free to use, modify, and distribute it under the terms of the license.