Sha256: 4db1edcaff986c69420f995429afd24006e274e1c821238461ed12910fe06af8

Contents?: true

Size: 888 Bytes

Versions: 395

Compression:

Stored size: 888 Bytes

Contents

pub struct Allergies(pub usize);

#[derive(PartialEq, Debug)]
pub enum Allergen { Eggs, Peanuts, Shellfish, Strawberries, Tomatoes, Chocolate, Pollen, Cats }

impl Allergies {
    pub fn new(score: usize) -> Allergies {
        Allergies(score)
    }

    pub fn is_allergic_to(&self, allergen: &Allergen) -> bool {
        let allergens = Allergies::allergens();
        let index = allergens.iter().position(|x: &Allergen| x == allergen).unwrap();
        (self.0 & (1 << index)) != 0
    }

    pub fn allergies(&self) -> Vec<Allergen> {
        Allergies::allergens().into_iter().filter(|allergen| self.is_allergic_to(allergen)).collect()
    }

    fn allergens() -> Vec<Allergen> {
        vec![Allergen::Eggs, Allergen::Peanuts, Allergen::Shellfish,
            Allergen::Strawberries, Allergen::Tomatoes, Allergen::Chocolate,
            Allergen::Pollen, Allergen::Cats]
    }
}

Version data entries

395 entries across 395 versions & 1 rubygems

Version Path
trackler-2.2.1.179 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.178 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.177 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.176 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.175 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.174 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.173 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.172 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.171 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.170 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.169 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.167 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.166 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.165 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.164 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.163 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.162 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.161 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.160 tracks/rust/exercises/allergies/example.rs
trackler-2.2.1.159 tracks/rust/exercises/allergies/example.rs