Introduction
TheVec
class in Rust is a dynamically sized, growable array type. It allows you to store a collection of elements of the same type and provides methods to manipulate these elements efficiently. Vec
is a cornerstone of Rust's standard library, widely used for various purposes due to its flexibility and performance.
Using Vec
Creating and using aVec
is straightforward. You can initialize an empty vector or one with predefined elements, and then use various methods to manipulate it.
fn main() {
let mut vec = Vec::new(); // Create a new, empty Vec
vec.push(1); // Add an element to the Vec
vec.push(2);
vec.push(3);
for element in &vec {
println!("{}", element);
}
}
// Output:
// 1
// 2
// 3
Key methods
Below are some of the key methods exposed by the Vec
class:
new
Creates a new, emptyVec
.
fn main() {
let vec: Vec<i32> = Vec::new();
println!("{:?}", vec); // Output: []
}
with_capacity
Creates a new, emptyVec
with a specified capacity.
fn main() {
let vec: Vec<i32> = Vec::with_capacity(10);
println!("{:?}", vec); // Output: []
}
push
Adds an element to the end of theVec
.
fn main() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
vec.push(3);
println!("{:?}", vec); // Output: [1, 2, 3]
}
pop
Removes and returns the last element from theVec
, or None
if it is empty.
fn main() {
let mut vec = vec![1, 2, 3];
let last = vec.pop();
println!("{:?}", last); // Output: Some(3)
println!("{:?}", vec); // Output: [1, 2]
}
len
Returns the number of elements in theVec
.
fn main() {
let vec = vec![1, 2, 3];
println!("{}", vec.len()); // Output: 3
}
is_empty
Returnstrue
if the Vec
contains no elements.
fn main() {
let vec: Vec<i32> = Vec::new();
println!("{}", vec.is_empty()); // Output: true
}
insert
Inserts an element at a specified index, shifting elements after it to the right.fn main() {
let mut vec = vec![1, 2, 3];
vec.insert(1, 4);
println!("{:?}", vec); // Output: [1, 4, 2, 3]
}
remove
Removes and returns the element at a specified index, shifting elements after it to the left.fn main() {
let mut vec = vec![1, 2, 3];
let removed = vec.remove(1);
println!("{:?}", removed); // Output: 2
println!("{:?}", vec); // Output: [1, 3]
}
contains
Checks if theVec
contains a given element.
fn main() {
let vec = vec![1, 2, 3];
println!("{}", vec.contains(&2)); // Output: true
println!("{}", vec.contains(&4)); // Output: false
}
Example Usage
Example 1: Creating and populating a Vec
fn main() {
let mut vec = Vec::new();
vec.push(10);
vec.push(20);
vec.push(30);
println!("{:?}", vec); // Output: [10, 20, 30]
}
Example 2: Iterating over elements
fn main() {
let vec = vec![1, 2, 3, 4, 5];
for element in &vec {
println!("{}", element);
}
}
// Output:
// 1
// 2
// 3
// 4
// 5
Example 3: Using methods to manipulate Vec
fn main() {
let mut vec = vec![1, 2, 3];
vec.push(4);
vec.insert(1, 5);
let removed = vec.remove(2);
println!("{:?}", vec); // Output: [1, 5, 3, 4]
println!("{:?}", removed); // Output: 2
}
Example 4: Checking for elements
fn main() {
let vec = vec![1, 2, 3];
println!("{}", vec.contains(&2)); // Output: true
println!("{}", vec.contains(&4)); // Output: false
}
Example 5: Creating Vec with capacity
fn main() {
let mut vec = Vec::with_capacity(5);
vec.push(1);
vec.push(2);
println!("Length: {}, Capacity: {}", vec.len(), vec.capacity()); // Output: Length: 2, Capacity: 5
}
Considerations
- Vectors are heap-allocated, so the performance may vary depending on the size of the data.
- Use
Vec::with_capacity
to avoid reallocations if the size of the vector is known in advance. - Vectors are zero-indexed. Accessing elements outside the valid range will cause a panic.