HashMap multi-type enum keys

Published 528d ago

 ayebear

use std::collections::HashMap;

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

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

    println!("{:?}", map);
    println!("{}", map.get(&Color::Other(128, 128, 128)).unwrap());
    println!("{}", map.get(&Color::Other(10, 10, 10)).is_none())
}

1

Please login or sign up to comment and collaborate