I am very confused why the last test in the “Allergies” Exercise does not contain “Peanuts”.
#[test]
fn scores_over_255_do_not_trigger_false_positives() {
let expected = &[
Allergen::Eggs,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats,
];
let allergies = Allergies::new(509).allergies();
compare_allergy_vectors(expected, &allergies);
}
It has everything except Peanuts…
In this solution, all I’m doing is checking that the allergen exists and is not equal to zero… huh? Shouldn’t I be decrementing the allergen points from the score or something? What is the relationship here between score and allergens? I though if the score is greater than the allergen value then the person is allergic…
pub struct Allergies {
score: u8,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Allergen {
Eggs = 1,
Peanuts = 2,
Shellfish = 4,
Strawberries = 8,
Tomatoes = 16,
Chocolate = 32,
Pollen = 64,
Cats = 128,
}
impl Allergies {
pub fn new(score: u32) -> Self {
Self { score: score as u8 }
}
pub fn is_allergic_to(&self, allergen: &Allergen) -> bool {
self.score & *allergen as u8 != 0
}
pub fn allergies(&self) -> Vec<Allergen> {
let all_allergens = vec![
Allergen::Eggs,
Allergen::Peanuts,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats,
];
all_allergens
.into_iter()
.filter(|allergen| self.is_allergic_to(allergen))
.collect()
}
}
Also, if my solution passes all tests why can’t I share it?
And finally I am posting here asking for help because the lame points system will not let me get any more mentoring…