liveBook · Manning

Vector

As explained in Code Like a Pro in Rust

A vector is an ordered, indexed sequence of elements, often numerical, represented as a mathematical quantity or a computational collection.

Use `Vec<T>` as the default container for a resizable sequence of values. It stores elements in contiguous, heap-allocated memory. Prefer it for a list of items; use `Box<T>` when you need to allocate a single item. section 4.9 section 5.10

A vector can be borrowed as a slice, `&[T]`, through `Deref`; a mutable borrow can similarly become `&mut [T]`. The slice has a fixed length, so it cannot resize the vector. The borrow checker also prevents resizing a vector while a borrowed slice remains in use. section 4.3.1

A slice blocks resizing

Given `let mut v = vec![1, 2, 3]; let slice = v.as_slice();`, calling `v.resize(10, 0)` before the last use of `slice` fails to compile: resizing needs a mutable borrow while the slice holds an immutable one. section 4.3.1

Capacity is measured in elements, not bytes, and can exceed length. An empty vector starts with zero capacity and allocates when elements are added; reaching capacity triggers growth. If you know the required size, start with `Vec::with_capacity(n)` to avoid repeated reallocations. That reserves space—it does not add elements. For a large vector retaining excess capacity, consider `shrink_to_fit()`, while weighing the cost of contiguous storage. section 11.2.1 section 11.7

Copying `Copy` elements

For equal-length source and destination sequences whose elements implement `Copy`, `copy_from_slice()` offers a fast copy path. In one release-mode benchmark copying 10 million `i32` values, it took about 0.0036 seconds versus about 0.025 seconds for a push-based loop; treat those timings as an example, not a guarantee. section 11.2.3

Start with `Vec` unless a specific access pattern or measured memory cost calls for another collection. `String` wraps `Vec<u8>`, and `BinaryHeap` uses a vector internally; `Vec<Box<T>>` is an option when storing large elements contiguously is a concern. section 4.3.2 section 4.3.3

How Code Like a Pro in Rust covers it

How deep it goes
Explains slice coercion, borrow restrictions, capacity growth, and copying optimizations using implementation snippets and benchmarks.
Examples
Shows a rejected resize during a slice borrow, observed capacity growth, and a release-mode copy benchmark.
What it assumes
Assumes basic Rust syntax while demonstrating borrow-checker restrictions and allocation behavior.
What you can do after
Apply `Vec::with_capacity` when size is known, and use `copy_from_slice` for eligible copies.
How it connects
Connects vectors to slices, `Deref`, `String`, `Box`, and alternative standard-library collections.

Also explained in

Compare how 11 books explain vector →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.