You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Rust's standard library provides powerful collection types that store data on the heap, along with an iterator system that enables functional-style data processing with zero-cost abstractions.
A Vec<T> is a growable, heap-allocated array:
// Creating vectors
let mut v: Vec<i32> = Vec::new();
let v2 = vec![1, 2, 3]; // macro shorthand
// Adding elements
v.push(1);
v.push(2);
v.push(3);
// Accessing elements
let third = &v[2]; // panics if out of bounds
let third = v.get(2); // returns Option<&i32>
| Method | Description |
|---|---|
push(value) | Add an element to the end |
pop() | Remove and return the last element |
len() | Number of elements |
is_empty() | Check if empty |
contains(&value) | Check if a value exists |
sort() | Sort in place |
dedup() | Remove consecutive duplicates |
retain(|x| pred) | Keep only elements matching a predicate |
iter() | Create an iterator |
let v = vec![1, 2, 3];
// Immutable iteration
for val in &v {
println!("{val}");
}
// Mutable iteration
let mut v = vec![1, 2, 3];
for val in &mut v {
*val *= 2;
}
A hash map stores key-value pairs:
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Alice"), 100);
scores.insert(String::from("Bob"), 85);
// Accessing values
let alice_score = scores.get("Alice"); // returns Option<&i32>
// Default insert — only insert if the key does not exist
scores.entry(String::from("Carol")).or_insert(90);
// Update based on existing value
let count = scores.entry(String::from("Alice")).or_insert(0);
*count += 10;
| Method | Description |
|---|---|
insert(key, value) | Insert or update a key-value pair |
get(&key) | Get an Option<&V> for a key |
contains_key(&key) | Check if a key exists |
remove(&key) | Remove a key-value pair |
len() | Number of entries |
keys() / values() | Iterate over keys or values |
entry(key) | Entry API for conditional insertion |
iter() | Iterate over (key, value) pairs |
String is a growable, UTF-8 encoded string:
let mut s = String::from("hello");
s.push(' ');
s.push_str("world");
println!("{s}"); // "hello world"
// Concatenation
let s1 = String::from("hello");
let s2 = String::from(" world");
let s3 = s1 + &s2; // note: s1 is moved
// format! macro (does not take ownership)
let s1 = String::from("hello");
let s2 = String::from("world");
let s3 = format!("{s1} {s2}");
Tip: Rust strings are UTF-8 encoded, so you cannot index into them by byte position (
s[0]is not allowed). Use.chars()or.bytes()to iterate.
Iterators are Rust's way of processing sequences of elements. They are lazy — they do nothing until consumed.
trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
let v = vec![1, 2, 3];
let iter = v.iter(); // yields &T (immutable references)
let iter = v.iter_mut(); // yields &mut T (mutable references)
let iter = v.into_iter(); // yields T (takes ownership)
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.