Vector Type
Unlike arrays, a vector stores the size of the array as well. Arrays don't need to store their size. That is why we can access an array element even when we exceed the actual capacity. The boundaries are not defined.
Another thing about vectors is that they're generally stored in a heap, and they have a larger size allocated than is being used. As a result, when new values are inserted, the whole vector does not need to be relocated to fulfill the size requirements.
As with arrays, both arrays and vectors copy items into another larger array/vector once more element(s) are added; however, vectors do the copying less often as they already have more capacity than the actual length.
Note: the vectors in both the figures are the same on high level, but have different names (
a
andb
) because they are different on the low level in the sense that they have different locations in memory.
"A vector is a contiguous growable array type with heap-allocated contents."1
Initializing a vector
While initializing a primitive array, we had to specify the size of the array before-hand. There is no such constraints with a vector
#![allow(unused)] fn main() { // vector from inbuilt macro let vec:Vec <u16> = vec![10, 20, 30, 40, 50]; println!("Vector array: {:?}", vec); // vector from array let vec = Vec::from([1, 2, 3, 4, 5]); println!("Vector array: {:?}", vec); // empty vector let mut _vec: Vec<u8> = Vec::new(); }
Accessing and modifying a vector
Similar to the array, we can access individual elements of a vector using its index and modify them if the vector is mutable.
#![allow(unused)] fn main() { let mut vec = vec![10, 20, 34, 40, 50]; // accessing an element let mid = vec[2]; println!("Mid element: {mid}"); // modifying an element vec[2] = 30; println!("Modified vector: {:?}", vec); }
Now, let's delve into some of the vector specific functions that will be helpful in learning and applying algorithms.
Vector specific functions
push()
: This function inserts an element at the end of the vectorpop()
: This function removes an element from the end of the vectorlen()
: This function outputs the number of elements in the vector
We'll be using these functions extensively. See docs for all the functions.
Advantages and disadvantages of vectors
Advantages | Disadvantages |
---|---|
Dynamic size | Slower access due to indirection |
Easy insertion and deletion | More memory usage due to extra capacity |
Efficient use of memory | More complex implementation |
Flexibility in size | Potential for reallocation |
2D vector
As with arrays, vectors are are also two dimensional. The idea of a 2D vector is the same of a 2D array.
Initializing a 2D array:
#![allow(unused)] fn main() { // empty 2D vector let mut _vec: Vec<Vec<char>> = Vec::new(); // method 1: let vec: Vec<Vec<u8>> = Vec::from([Vec::from([1, 2, 3]), Vec::from([4, 5, 6])]); println!("2d vector: {:?}", vec); // method 2: let vec: Vec<Vec<u8>> = vec![vec![1, 2, 3], vec![4, 5, 6]]; println!("2d vector: {:?}", vec); }
Notice that we have initialized the type inside the fish as another vector. And inside that vector, we have specified the data type.