// tag::build_allergen_map[]
/// Constructs a map with all allergens as keys and sets of candidate ingredients as values
:build_allergen_map[]
Constructs a map with all allergens as keys and sets of candidate ingredients as values
: build_allergen_map [] Constructs a map with all allergens as keys and sets of candidate ingredients as values
pub fn build_allergen_map<'a>(foods: &[(HashSet<&'a str>, HashSet<&'a str>)])
                              -> HashMap<&'a str, HashSet<&'a str>> {
    let mut allergen_map: HashMap<&str, HashSet<&str>> = HashMap::new();
    for (ingredients, allergens) in foods {
        for allergen in allergens {
            if let Some(candidates) = allergen_map.get_mut(allergen) {
                candidates.retain(|c| ingredients.contains(c));
            } else {
                let candidates: HashSet<_> =
                    ingredients.iter().map(|v| *v).collect();
                allergen_map.insert(allergen, candidates);
            }
        }
    }
    allergen_map
}
pub fn build_allergen_map < ' a > ( foods : & [ ( HashSet < & ' a str > , HashSet < & ' a str > ) ] ) -> HashMap < & ' a str , HashSet < & ' a str > > { let mut allergen_map : HashMap < & str , HashSet < & str > > = HashMap :: new ( ) ; for ( ingredients , allergens ) in foods { for allergen in allergens { if let Some ( candidates ) = allergen_map . get_mut ( allergen ) { candidates . retain ( | c | ingredients . contains ( c ) ) ; } else { let candidates : HashSet < _ > = ingredients . iter ( ) . map ( | v | * v ) . collect ( ) ; allergen_map . insert ( allergen , candidates ) ; } } } allergen_map }
tag::build_allergen_map[]
Constructs a map with all allergens as keys and sets of candidate ingredients as values
tag :: build_allergen_map [] Constructs a map with all allergens as keys and sets of candidate ingredients as values
foods
&[(HashSet<&'a str>, HashSet<&'a str>)]