DEV Community

El Bruno for Microsoft Azure

Posted on • Originally published at on

#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);

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode

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

Top comments (2)

pauljlucas profile image
Paul J. Lucas

This is mistagged. #c is the tag for C. You want #csharp.

elbruno profile image
El Bruno

Thanks ! I'll fix it !

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git