Expand description

An HashVec is a hash map / dictionary whose key-value pairs are stored (and can be iterated over) in a fixed order, by default the order in which they were inserted into the hashvec. It’s essentially a vector whose values can be inserted/retrieved with keys.

Example

// Create a new hashvec containing pairs of animal names and species
let mut hashvec: HashVec<&'static str, &'static str> = HashVec::new();
 
// Insert values into the hashvec
hashvec.insert("Doug", "Kobold");
hashvec.insert("Skye", "Jaguar");
hashvec.insert("Lee", "Shiba");
hashvec.insert("Sock", "Man");
hashvec.insert("Salad", "Dog");
hashvec.insert("Finn", "Human");
hashvec.insert("Jake", "Dog");
 
// Access a value by key
match hashvec.get("Finn") {
    Some(value) => {
        assert_eq!(*value, "Human");
    },
    None => {}
}
 
// Access an entry by index
let lee_value = hashvec[2];
assert_eq!(lee_value, ("Lee", "Shiba"));
 
// Get the index of a key
let lee_index = hashvec.index("Lee").unwrap();
assert_eq!(lee_index, 2);
 
// Mutate a value
match hashvec.get_mut("Sock") {
    Some(value) => {
        *value = "Guinea Pig";
    },
    None => {}
}
assert_eq!(*hashvec.get("Sock").unwrap(), "Guinea Pig");
 
// Remove a value
hashvec.remove("Doug");
assert_eq!(hashvec.get("Doug"), None);
 
// Iterate over each of the key-value pairs in the hashvec
for (k, v) in hashvec.into_iter() {
    println!("{} is a {}!", k, v);
}
 
// Clear the hashvec
map.clear();

Structs