capypad
0 day streak
rust / beginner
Snippet

Using Vectors for Dynamic Lists

Vectors allow you to store a variable number of values next to each other in memory. Unlike arrays, they can grow or shrink.

snippet.rs
rust
1
2
3
4
5
6
let mut v: Vec<i32> = Vec::new();
v.push(5);
v.push(6);
 
let second = v[1];
println!("The second element is {}", second);
Breakdown
1
Vec<i32> = Vec::new()
Creates a new, empty vector that will hold 32-bit integers.
2
v.push(5)
Adds a new element to the end of the vector.
3
v[1]
Accesses an element at a specific index using indexing syntax.