std::collection::HashMap collection in Rust.
Disclaimer: I'm by no means a Rust expert -- like a few of you out there, I'm merely a professional web developing mortal. My only goal is to not misguide any potential readers too badly during the process of me figuring these things out for myself. You've been warned.
What exactly is a hash map?
If you have a pretty good idea of what a hash map is, you can skip on down to the next section.
In general terms, a hash map is a type of data structure that allows you to create an association between a
key (Generally a character or string) and a
value (potentially anything). Imagine a spreadsheet or table with the following structure:
This table creates an association between a name (in hash map terms our
key) and a phone number (the
value). This is the basic structure of the "map" part of "hash map". The "hash" part has to do with how the association can be managed in a way that allows for efficient operations like look-ups and modifications to the key-value pairings. Each key (like individual 'Names' in the example above) is passed through whats called a "hashing function" in order to get a unique number that is explicitly tied to the original
key string. The
value can then be stored in an array, at the index that was computed by the hashing function. The table below illustrates this new structure:
The implementation of hash maps varies greatly between different programming languages, but generally the advantage of a hash map is that it provides very fast look-ups and very fast insertions. However, there are lots of nuances to this data structure, and components of hash maps like the hashing function can be implemented in many different ways. There are also other specifics, like how to handle cases where the hashing function returns the same index for multiple
key strings (aka "collisions") that won't be covered in this post.
Before we dive in to the specifics of
HashMap in Rust, I think it's only responsible to list just a few of the other names that may refer to the general concept of hash maps in the context of other programming languages (hopefully some of these make sense if they didn't before):
- Hash Table
- Symbol Table
- Associative Array
Keep in mind that there are nuances to these concepts, and they're not all technically referring to the same thing, but that's about as much detailed computer science as you're going to get out of a web developer.
HashMap in Rust
Vec, (which you can read all about in my very, very short overview)
HashMap is part of what Rust calls "collections".
Keys in Rust's hash maps can be many different types, including strings, integers, and interestingly, booleans. Like vectors, hash maps are dynamically sized and store their values on the heap at runtime.
Making your very own
HashMap can be done quite easily with the wonderfully convenient
HashMap::new(), or using
HashMap::with_capacity(uint) if you prefer a predetermined size for your hash maps. Another option is to start with a vector of tuples (like so:
[('key', 0)]) and using the handy
collect method. If you happened to have two vectors with
values just lying around, you could even use a combination of
zip (which creates a vector of tuples from two initial vectors) and
collect, as illustrated below:
let keys = vec![String::from("key"), String::from("key2")]; let vals = vec![1, 2]; let hash_map: HashMap<_, _> = keys.iter().zip(vals.iter()).collect();
Take note of the underscores in
HashMap<_, _>: basically this notation lets us use Rust's robust type inference to decide what the types of our
value will be without us explicitly saying so.
Reading and writing
HashMaps also conveniently allow us to insert and read values based on a
insert() methods respectively. Inserting into a hash map will overwrite the previous value, if one existed.
This post barely qualifies as an overview for Rust's
HashMap, but as always if you're interested in learning more about them from people much more qualified from me, check out some of the resources below:
- The Book - HashMaps
- Rust by Example - HashMaps
- Rust Documentation - Std::collections::HashMap
- Building a HashMap in Rust
Thanks for reading.