January 2019
Intermediate to advanced
316 pages
8h 8m
English
One major upside of using array-type data allocation is the simple and fast element access, which Vec<T> and VecDeque<T> share. The formal way to implement the direct access using brackets (let my_first_element= v[0];) is provided by the Index<I> trait.
Other than direct access, iterators are provided to search, fold, map, and so on the data. Some are equivalent to the LinkedList<T> part of this section.
As an example, the Vec<T>'s owning iterator (IntoIter<T>) owns the pointer to the buffer and moves a pointer to the current element forward. There is also a catch though: if the size of an element is zero bytes, how should the pointer be moved? What data is returned? The IntoIter<T> structure comes up with a clever solution (ZSTs ...