Tsallaka zuwa abun ciki
Justice Tech School

Vectors and HashMaps

Vec<T> is a growable list and HashMap stores key value pairs. Iterators with map, filter and collect transform data in a functional style.

Example
use std::collections::HashMap;
let mut v = vec![1, 2, 3];
v.push(4);
let doubled: Vec<i32> = v.iter().map(|n| n * 2).collect();
println!("{:?}", doubled);
let evens: Vec<&i32> = v.iter().filter(|n| *n % 2 == 0).collect();
println!("{:?}", evens);
let mut ages = HashMap::new();
ages.insert("Ada", 21);
println!("{:?}", ages.get("Ada"));

Result

[2, 4, 6, 8]
[2, 4]
Some(21)

Exercise

Create a growable list.

let v = ![1, 2];

Quiz Test

Which type is a growable list?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.