This crate provides the
SegVec data structure.
It is similar to
Vec, but allocates memory in chunks of increasing size, referred to as
“segments”. This involves a few trade-offs:
- Element addresses are stable across
pushoperations even if the
- Resizing only allocates the additional space needed, and doesn’t require copying.
- Operations are slower (some, like
drain, are much slower) than for a
Vecdue to the need for multiple pointer dereferences and conversion between linear indexes and
- Direct slicing is unavailable (i.e. no
&mut [T]), though
- You have a long-lived
Vecwhose size fluctuates between very large and very small throughout the life of the program.
- You have a large append-only
Vecand would benefit from stable references to the elements
Vecto store the list of segments, allowing the first few segment headers to live on the stack. Can speed up access for small
Vecto store the data for each segment, meaning that each segment header takes up the space of a single
usize, rathern than 3 when using
Removes and returns elements from a range in a
Any un-consumed elements are removed and dropped when a
Drain is dropped.
Drain is forgotten (via
std::mem::forget), it is unspecified how many elements are
removed. The current implementation calls
SegVec::remove on a single element on each call to
A data structure similar to
Vec, but that does not copy on re-size and can
release memory when it is truncated.