Blue: 10
Yellow: 50
()
Performant Software Systems with Rust — Lecture 7
Vectors: Vec<T>
Growable stores on the heap that contain multiple values of the same type
String
Reading Elements of a Vector
{
let v = vec![1, 2, 3, 4, 5];
let third: &i32 = &v[2]; // panics if the index is out of bounds
println!("The third element is {third}");
let third: Option<&i32> = v.get(2); // returns None, no panic!
match third {
Some(third) => println!("The third element is {third}"),
None => println!("There is no third element."),
}
} // v goes out of scope and is freed here
Learn more: Storing Lists of Values with Vectors
Enforcing Ownership Rules
Learn more: The Rustonomicon: Implementing Vec<T>
Iterating Over the Values in a Vector
Using an Enum to Store Multiple Types in a Vector
Learn more: Iterating Over the Values in a Vector
Hash Maps: HashMap<K, V>
Are Key-Value Stores
K
(same type) to values of type V
(same type) on the heap
Inserting Key-Value Pairs into Hash Maps
Updating a Hash Map by Replacing a Value
Adding a Key and Value Only If a Key Is Not Present
Updating a Value Based on the Old Value
The Rust Programming Language, Chapter 8