9951 explained code solutions for 126 technologies


rustHow to use an enum in a Rust HashMap?


Enums can be used as keys in a Rust HashMap. To do this, the enum must implement the Eq and Hash traits.

Example code

use std::collections::HashMap;

#[derive(Eq, Hash, PartialEq)]
enum Color {
    Red,
    Blue,
    Green,
}

fn main() {
    let mut colors = HashMap::new();
    colors.insert(Color::Red, "red");
    colors.insert(Color::Blue, "blue");
    colors.insert(Color::Green, "green");

    println!("{:?}", colors);
}

Output example

{Color::Red: "red", Color::Blue: "blue", Color::Green: "green"}

Code explanation

  1. #[derive(Eq, Hash, PartialEq)]: This line is used to derive the Eq, Hash, and PartialEq traits for the Color enum. This is necessary for the enum to be used as a key in a HashMap.

  2. let mut colors = HashMap::new();: This line creates a new empty HashMap.

  3. colors.insert(Color::Red, "red");: This line inserts a key-value pair into the HashMap. The key is the Color::Red enum variant, and the value is the string "red".

Helpful links

Edit this code on GitHub