arrlist
A generic, heap-allocated dynamic array for no_std environments.
Under the hood
ArrayList<T> uses Box<[MaybeUninit<T>]> as its backing store, which means:
Tnever needs to implementDefault- Uninitialized slots are never read as
T(no UB lurking around) - Drop glue only runs on elements that were actually written
Features at a glance
no_stdcompatible — needsalloc, nothing else- Amortized O(1) push/pop — capacity doubles from 4, like
Vec - O(n) insert, remove, pop_front — with proper shifting
- Three iterator flavors — owned (
IntoIter), borrowed (Iter), and mutable (IterMut) - Built-in algorithms — bubble sort, reverse, linear search, binary search
- Multiple constructors — from
Vec<T>,[T; N],&[T], or from scratch - Friendly
arrlist![]macro — mirrors thevec![]syntax you already know
Installation
Add this to your Cargo.toml:
[]
= "0.1.5"
Quick Start
use ;
// The macro works just like vec![]
let mut list = arrlist!;
assert_eq!;
assert_eq!;
// Push and pop from the back
list.push.unwrap;
assert_eq!;
// Pre-allocate with a known capacity
let zeros: = arrlist!;
assert_eq!;
The arrlist![] macro
Three forms, mirroring vec![]:
use ;
// Empty list — no heap allocation yet
let empty: = arrlist!;
// Explicit elements
let nums = arrlist!;
// N copies of a value (value must implement Clone)
let repeated = arrlist!;
Constructors
use ArrayList;
// Start empty, allocate on first push
let list: = new;
// Reserve space upfront
let list: = with_capacity;
// From an array (moves elements, no clone)
let list = from_array;
// From a Vec (reuses the buffer, no copy)
let list = from;
// From a slice (clones each element; T: Clone required)
let list = from_slice;
Core Operations
use ArrayList;
let mut list = from_array;
// Read
assert_eq!;
assert_eq!;
// Write
list.set.unwrap;
// Modify in place
if let Some = list.get_mut
// Insert at an arbitrary position (O(n))
list.insert.unwrap;
// Remove at an arbitrary position (O(n))
let removed = list.remove.unwrap;
// Pop from the back (O(1)) or front (O(n))
list.push.unwrap;
list.pop;
list.pop_front;
// Wipe everything (drops elements, keeps allocation)
list.clear;
Iterators
All three standard iterator patterns are supported:
use ArrayList;
let list = from_array;
// Shared references
for val in &list
// Mutable references
let mut list = from_array;
for val in &mut list
// Consuming the list
for val in list
Algorithms
use ArrayList;
let mut list = from_array;
// Reverse in place — O(n)
list.reverse;
// Bubble sort ascending — O(n²), fine for small or nearly-sorted data
list.sort;
// Linear search — O(n), works on any list
let idx = list.linear_search;
// Binary search — O(log n), list must be sorted first
let idx = list.binary_search;
Heads up on sort: The built-in sort uses bubble sort, which is O(n²). It's perfectly reasonable for small lists. If you're dealing with thousands of elements, extract to a
Vecand useslice::sort_unstableinstead.
Error Handling
Fallible operations return Result<_, ListError>. There are three variants:
| Variant | When you'll see it |
|---|---|
ListError::EmptyList |
Index-based op on an empty list |
ListError::OutOfBounds { idx, limits } |
Index out of range on a non-empty list |
ListError::CapacityOverflow |
Growing would overflow usize (practically unreachable on 64-bit) |
use ;
let mut list: = new;
match list.remove
list.push.unwrap;
list.push.unwrap;
match list.set
Display
ArrayList<T> implements Display when T: Debug, printing in the familiar [a, b, c] format:
use ArrayList;
let list = from_array;
println!; // [1, 2, 3]
no_std Setup
In your crate root, enable the alloc extern and make sure your target provides an allocator:
extern crate alloc;
use vec;
use ArrayList;
Running the Tests
The test suite covers construction, element operations, iterators, algorithms, error paths, and macro behavior.
License
MIT — see LICENSE for details.