Rust - Vectors Flashcards
Describe Vectors
A Vector is a resizable array.
A Vector can grow or shrink at runtime.
A Vector is a homogeneous collection.
A Vector stores data as sequence of elements in a particular order.
A Vector will only append values to (or near) the end. In other words, a Vector can be used to implement a stack.
Memory for a Vector is allocated in the heap.
Syntax for creating vectors
let mut instance_name = Vec::new();
Mention Vec methods
New
push()
remove()
contains()
Len()
How do you create a new vector and add items to it
fn main() {
let mut v = Vec::new();
v.push(20);
v.push(30);
v.push(40);
println!(““,v);
}
How do you access values from a vector
fn main() {
let mut v = Vec::new();
v.push(20);
v.push(30);
v.push(40);
v.push(500);
for i in &v {
println!(“{}”,i);
}
println!(““,v);
}