Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created September 18, 2015 18:58
Show Gist options
  • Select an option

  • Save steveklabnik/12ef5e7a9ea7aa14f437 to your computer and use it in GitHub Desktop.

Select an option

Save steveklabnik/12ef5e7a9ea7aa14f437 to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
use std::fmt;
#[derive(PartialEq, Eq, Hash, Debug, Copy, Clone)]
pub enum Allergen {
Eggs = 1,
Strawberries = 8,
Peanuts = 2,
Shellfish = 4,
Chocolate = 32,
Pollen = 64,
Cats = 128,
Tomatoes = 16
}
impl Allergen {
fn is_allergic_to(allergen: Allergen) -> bool {
allergen as i32 >= 1
}
}
impl fmt::Display for Allergen {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({})", self)
}
}
type Allergies = HashMap<Allergen, i32>;
pub fn Allergies(num: i32) -> Vec<Allergen> {
let mut result = vec![];
let mut allergies = HashMap::new();
allergies.insert(Allergen::Eggs, 1);
allergies.insert(Allergen::Peanuts, 2);
allergies.insert(Allergen::Shellfish, 4);
allergies.insert(Allergen::Strawberries, 8);
allergies.insert(Allergen::Tomatoes, 16);
allergies.insert(Allergen::Chocolate, 32);
allergies.insert(Allergen::Pollen, 64);
allergies.insert(Allergen::Cats, 128);
// LIFETIMES! ARGHGH!!
for (key, value) in allergies {
println!("key: {} val: {}", key, value);
if value >= num {
result.push(key)
}
}
result
}
use std::collections::HashMap;
use std::fmt;
#[derive(PartialEq, Eq, Hash, Debug, Copy, Clone)]
pub enum Allergen {
Eggs = 1,
Strawberries = 8,
Peanuts = 2,
Shellfish = 4,
Chocolate = 32,
Pollen = 64,
Cats = 128,
Tomatoes = 16
}
impl Allergen {
fn is_allergic_to(allergen: Allergen) -> bool {
allergen as i32 >= 1
}
}
impl fmt::Display for Allergen {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({})", self)
}
}
type Allergies = HashMap<Allergen, i32>;
pub fn Allergies(num: i32) -> Vec<Allergen> {
let mut result = vec![];
let mut allergies = HashMap::new();
allergies.insert(Allergen::Eggs, 1);
allergies.insert(Allergen::Peanuts, 2);
allergies.insert(Allergen::Shellfish, 4);
allergies.insert(Allergen::Strawberries, 8);
allergies.insert(Allergen::Tomatoes, 16);
allergies.insert(Allergen::Chocolate, 32);
allergies.insert(Allergen::Pollen, 64);
allergies.insert(Allergen::Cats, 128);
// LIFETIMES! ARGHGH!!
for (key, value) in allergies {
println!("key: {} val: {}", key, value);
if value >= num {
result.push(key)
}
}
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment