#Rust 🦀 – Working with hashmaps is cool. And a little C# experience 😄

Hi !

During today’s Rust lession in the “First Steps with Rust” (in Spanish), we reviewed the Hashmaps in Rust. We talk about the behavior when we access a non existing item, and it was nice to get a NONE return instead of an error or an exception.

In example this code:

use std::collections::HashMap;

fn main() {
    let mut pets = HashMap::new();
    pets.insert("Ace", "dog");
    pets.insert("Goku", "car");
    pets.insert("Jim", "squirrel");
    println!("{:#?}", pets);

    let net = "Net";
    let net_animal = pets.get(net);
    println!("{} is a {:?}", net, net_animal);

Returns this output.

warning: `HashMap01` (bin "HashMap01") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.83s
     Running `target\debug\HashMap01.exe`
    "Ace": "dog",
    "Goku": "car",
    "Jim": "squirrel",
Net is a None

There is no element with the key “Net”, so it returs None.

Is the same in C# ? 🤔

And, hey, what happens with this in C#? I was sure that, if I try to access a non-existing element, the app will trigger an error. So I tested this code

// create a new dictionary named petsDic
Dictionary<string, string> petsDic = new Dictionary<string, string>();
petsDic.Add("Ace", "dog");
petsDic.Add("Goku", "car");
petsDic.Add("Jim", "squirrel");

// print the petsDic items
foreach (KeyValuePair<string, string> pet in petsDic)
    Console.WriteLine(pet.Key + " is a " + pet.Value);

// get an element from the dictionary
var petD = petsDic["Milly"];
Console.WriteLine("Milly is a " + petD);

And, as expected, I got an System.Collections.Generic.KeyNotFoundException !

But hey, for this sample I’m using a Dictionary<>. So let’s give this a try using a Hashtable in C#. So let’s try this code.

using System.Collections;

// create a new hashtable named pets
Hashtable pets = new Hashtable();

pets.Add("Ace", "dog");
pets.Add("Goku", "car");
pets.Add("Jim", "squirrel");

// print the pets hashtable items
foreach (DictionaryEntry pet in pets)
    Console.WriteLine(pet.Key + " is a " + pet.Value);

// get an element from the hashtable
var petM = pets["Milly"];
Console.WriteLine("Milly is a " + petM);

And, this scenario didn’t trigger an Exception 👍.

using HashTables in C#, and accesing a non-exiting element, did not trigger an exception.

During this Rust series, we talk a lot about understanding how each data type works. And as a plus, I’ll add that if you have some previous knowledge, it’s better to check and validate the knowledge ! I was caught in a false assumption in this one !

Happy coding!


El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: