Vectors in Rust
Vectors in Rust are a dynamic array type provided by the standard library. They are implemented as the Vec<T>
type, where T
represents the type of elements stored in the vector. Vectors can grow or shrink in size and provide efficient indexing and iteration.
Creating a Vector
You can create a new vector using the Vec::new
method or the vec!
macro:
#![allow(unused)] fn main() { let mut v: Vec<i32> = Vec::new(); let v = vec![1, 2, 3, 4, 5]; }
Adding Elements
You can add elements to a vector using the push
method:
#![allow(unused)] fn main() { let mut v = Vec::new(); v.push(1); v.push(2); v.push(3); }
Accessing Elements
You can access elements in a vector using indexing or the get
method:
#![allow(unused)] fn main() { let v = vec![1, 2, 3, 4, 5]; println!("The third element is {}", v[2]); match v.get(2) { Some(third) => println!("The third element is {}", third), None => println!("There is no third element."), } }
Iterating Over a Vector
You can iterate over the elements of a vector using a for
loop:
#![allow(unused)] fn main() { let v = vec![1, 2, 3, 4, 5]; for i in &v { println!("{}", i); } }
Removing Elements
You can remove elements from a vector using the pop
method or the remove
method:
#![allow(unused)] fn main() { let mut v = vec![1, 2, 3, 4, 5]; v.pop(); // Removes the last element v.remove(1); // Removes the element at index 1 }
Vectors are a versatile and powerful collection type in Rust, suitable for many use cases where dynamic arrays are needed.