Rust - Vectors Flashcards

1
Q

Describe Vectors

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Syntax for creating vectors

A

let mut instance_name = Vec::new();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Mention Vec methods

A

New
push()
remove()
contains()
Len()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create a new vector and add items to it

A

fn main() {
let mut v = Vec::new();
v.push(20);
v.push(30);
v.push(40);

println!(““,v);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you access values from a vector

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly