23.07 Collections (Vectors, HashMaps, HashSets)

Collections (Vectors, HashMaps, HashSets)

Overview

Rust provides collections like Vectors, HashMaps, and HashSets for managing groups of data.

Topics

Examples

Vectors

let mut vec = Vec::new();
vec.push(1);
vec.push(2);
println!("{:?}", vec);

HashMaps

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
println!("{:?}", map);

HashSets

use std::collections::HashSet;

let mut set = HashSet::new();
set.insert(1);
set.insert(2);
set.insert(1); // Duplicate, won't be added
println!("{:?}", set);

Tags

#rust #collections #vectors #hashmaps #hashsets