len()
returns the number of elements in the vector (i.e., the vector's length). In the example below, vec
contains 5 elements, so len()
returns 5
.
capacity()
returns the number of elements the vector can hold (without reallocating memory). In the example below, vec
can hold 105
elements, since we use reserve()
to allocate at least 100 slots in addition to the original 5 (more might be allocated in order to minimize the number of allocations).
fn main() {
let mut vec = vec![1, 2, 3, 4, 5];
vec.reserve(100);
assert!(vec.len() == 5);
assert!(vec.capacity() >= 105);
}