Collections are data structures that live on the heap, so they are able to change in size
Vectors can contain a varying number of items of the same type
You can create a vector with values populated using the vec! macro:
let v = vec![1, 2, 3];
or an empty one:
let v: Vec<i32> = Vec::new();
You can add to a vector using push, and to do so the vector must be mutable:
let mut v = Vec::new();
v.push(5);
v.push(6);
v.push(7);
v.push(8);
let v = vec![1, 2, 3, 4, 5];
let third: &i32 = &v[2];
println!("The third element is {third}");
let third: Option<&i32> = v.get(2);
match third {
Some(third) => println!("The third element is {third}"),
None => println!("There is no third element."),
}
Accessing via index will throw an error if the index is invalid, whereas get will not error but return None. This is the key difference between the two, so you should choose accordingly depending on whether it should reasonably be possible to access an invalid index or not
let v = vec![100, 32, 57];
for i in &v {
println!("{i}");
}