Hashmaps are a powerful data structure that allow you to store key-value pairs. In Rust, the HashMap
type is provided that uses the a hashing algorithm called SipHash to store keys and values in a way that allows for fast and secure lookups. Think of them as a dictionary in Python or an object in JavaScript.
In this challenge, we want to build a sanctuary registry that allows us to manage animals in different sections of the sanctuary. We'll use a HashMap
to store the sections as keys and a Vec
to store the animals in each section. Each key is a section name String
and each value is a list of animals in that section Vev<String>
.
You are given a type of Collection
which is a HashMap<String, Vec<String>>
. The key is the section name and the value is a list of animals in that section.
Your task is to implement the following functions:
add_animal_to_section
: This function should add an animal to a section in the registry. If the section does not exist, it should be created. If the animal is already in the section, it should not be added again.
get_animals_in_section
: This function should return a list of animals sorted alphabetically in a given section. If the section does not exist, it should return an empty list.
get_all_animals
: This function should return a copy of the entire registry with all animals sorted alphabetically in each section.
insert
method on a HashMap
to add a new section or update an existing section.entry
method on a HashMap
to get a mutable reference to a section.iter
method on a HashMap
to iterate over the key-value pairs.sort
method on a Vec
to sort the animals alphabetically.ad0x99
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let section_animals = registry.entry(section.to_string()).or_insert_with(Vec::new); if !section_animals.contains(&animal.to_string()) { section_animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry .get(section) .cloned() .unwrap_or_else(Vec::new); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry .values() .flat_map(|section| section.clone()) .collect(); animals.sort(); animals}
fdumontmd
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal = animal.to_string(); let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal) { animals.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(§ion.to_string()).cloned().unwrap_or(Vec::new()); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<_> = registry.values().flatten().cloned().collect(); animals.sort(); animals}
sander-b-postnl
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let entry = registry.entry(section.to_string()).or_insert_with(Vec::new); if !entry.contains(&animal.to_string()) { entry.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).cloned().unwrap_or_else(Vec::new); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry.values().flatten().cloned().collect(); all_animals.sort(); all_animals}
savasozturk
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry .get(§ion.to_string()) .cloned() .unwrap_or_else(Vec::new); animals.sort(); return animals;}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry.values().flat_map(|v| v.iter()).cloned().collect(); all_animals.sort(); return all_animals;}
jose-bernardo
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry.entry(section.to_string()) .and_modify(|section_animals| { if !section_animals.contains(&animal.to_string()) { section_animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut section_animals = registry.get(section).cloned().unwrap_or_else(Vec::new); section_animals.sort(); section_animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry.values() .flat_map(|vec| vec) .cloned() .collect(); all_animals.sort(); all_animals}
jose-bernardo
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry.entry(section.to_string()) .and_modify(|section_animals| { if !section_animals.contains(&animal.to_string()) { section_animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut section_animals = registry.get(section).cloned().unwrap_or_else(Vec::new); section_animals.sort(); println!("{:?}", section_animals); section_animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry.values() .flat_map(|vec| vec) .cloned() .collect(); all_animals.sort(); println!("{:?}", all_animals); all_animals}
mk-comm
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;/// Adds an animal to a specific section in the registry, avoiding duplicates.pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let entry = registry.entry(section.to_string()).or_insert_with(Vec::new); if !entry.contains(&animal.to_string()) { entry.push(animal.to_string()); }}/// Retrieves a list of animals in a specific section, sorted alphabetically.pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).cloned().unwrap_or_else(Vec::new); animals.sort(); animals}/// Retrieves a sorted list of all animals across all sections.pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry.values().flatten().cloned().collect(); all_animals.sort(); all_animals}
digitalresistor
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).cloned().unwrap_or_default().to_vec(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().cloned().flatten().collect(); animals.sort(); animals}
digitalresistor
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).cloned().unwrap_or(Vec::new()).to_vec(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().cloned().flatten().collect(); animals.sort(); animals}
digitalresistor
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry .get(section).cloned() .unwrap_or(vec![]) .to_vec(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = registry .values() .fold(Vec::new(), |mut acc, animals| { acc.extend_from_slice(animals); acc }) .to_vec(); animals.sort(); animals}
digitalresistor
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry .get(§ion.to_string()) .unwrap_or(&vec![]) .to_vec(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = registry .values() .fold(Vec::new(), |mut acc, animals| { acc.extend_from_slice(animals); acc }) .to_vec(); animals.sort(); animals}
Mxn-ptr
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let section_list = registry.entry(section.to_string()).or_insert_with(Vec::new); if !section_list.contains(&animal.to_string()) { section_list.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut values = registry.get(section).cloned().unwrap_or_else(Vec::new); values.sort(); return values;}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut values: Vec<String> = registry.values() .flat_map(|animals| animals) .cloned() .collect(); values.sort(); values }
Mxn-ptr
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let section_list = registry.entry(section.to_string()).or_insert_with(Vec::new); if !section_list.contains(&animal.to_string()) { section_list.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(§ion.to_string()) { Some(value) => { let mut cloned_values = value.to_vec(); cloned_values.sort(); cloned_values } None => Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut values: Vec<String> = registry.values() .flat_map(|animals| animals) .cloned() .collect(); values.sort(); values }
hinphansa
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let section = registry.entry(section.to_string()).or_insert(vec![]); if !section.contains(&animal.to_string()) { section.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.get(section).cloned().unwrap_or_else(Vec::new); animals.sort(); return animals;}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = vec![]; for (_, animals) in registry.iter() { all_animals.extend(animals.clone()); } all_animals.sort(); return all_animals;}
hinphansa
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function if registry.contains_key(section) { let animals = registry.get_mut(section); match animals { Some(animals) => { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } } None => {} } } else { registry.insert(section.to_string(), vec![animal.to_string()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function return match registry.get(section) { Some(animals) => { let mut res = animals.clone(); res.sort(); res } None => vec![], };}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = vec![]; for (_, animals) in registry.iter() { all_animals.extend(animals.clone()); } all_animals.sort(); return all_animals;}
funny233-github
use std::collections::HashMap;use std::collections::HashSet;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry .entry(section.into()) .and_modify(|v| { if v.iter().find(|&x| x == animal).is_none() { v.push(animal.into()) } }) .or_insert(vec![animal.into()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut res = registry.get(section).unwrap_or(&vec![]).to_owned(); res.sort(); res}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = HashSet::new(); for (_, v) in registry { animals.extend(v.to_owned()) } let mut res: Vec<String> = animals.into_iter().collect(); res.sort(); res}
Aditeya
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let v = registry.entry(section.to_string()).or_insert(Vec::with_capacity(1)); let animal = animal.to_string(); if !v.contains(&animal) { v.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v = registry.get(section).cloned().unwrap_or_default(); v.sort(); v}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v: Vec<String> = registry.values().cloned().flatten().collect(); v.sort(); v}
jhq223
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|vec| { if !vec.contains(&animal.to_string()) { vec.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result = registry .get(section) .unwrap_or(&vec![]) .to_vec(); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = vec![]; for v in registry.values() { animals.extend_from_slice(v); } animals.sort(); animals}
xbarnett
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let v = registry.entry(section.to_string()).or_insert(Vec::new()); if !v.contains(&animal.to_string()) { v.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result = registry.get(§ion.to_string()).unwrap_or(&Vec::new()).clone(); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = Vec::new(); for v in registry.values() { result.extend(v.clone()); } result.sort(); result}
kyhou
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let list = registry.entry(section.to_string()).or_insert(vec![animal.to_string()]); if !list.contains(&animal.to_string()) { list.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(list) = registry.get(§ion.to_string()){ let mut vec = list.clone(); vec.sort(); return vec; } vec![]}//This function should return a copy of the entire registry with all animals sorted alphabetically in each section.pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut vec: Vec<String> = registry.values().flatten().cloned().collect(); vec.sort(); vec}
CianciuStyles
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let entry = registry .entry(section.to_string()) .or_insert(Vec::<String>::new()); let a = animal.to_string(); if !entry.contains(&a) { entry.push(a); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut collection = registry.get(section).cloned().unwrap_or_default(); collection.sort(); collection}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); animals}
masteryachty
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let vect = registry.entry(section.to_string()).or_insert(Vec::new()); if !vect.contains(&animal.to_string()) { vect.push(animal.to_string()); } }pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).cloned().unwrap_or_else(Vec::new); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = registry.values().flat_map(|v| v.iter()).cloned().collect(); animals.sort(); animals}
morigs
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry.entry(section.into()) .and_modify(|a| if !a.contains(&animal.to_string()) { a.push(animal.into()); }) .or_insert(vec![animal.into()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry .get(section.into()).cloned().unwrap_or_default(); animals.sort_unstable(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all = registry.values().flatten().cloned().collect::<Vec<String>>(); all.sort_unstable(); all}
dantekelly
use std::collections::{HashSet, HashMap};type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result: Vec<String> = registry.get(section).cloned().unwrap_or_default(); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); animals}
dantekelly
use std::collections::{HashSet, HashMap};type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(§ion.to_string()) { Some(r) => { let mut vr = r.to_vec(); vr.sort(); return vr; }, None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let vec_of_vect: Vec<Vec<String>> = registry.clone().into_values().collect(); let mut flat_vec: Vec<String> = vec_of_vect.into_iter().flatten().collect(); flat_vec.sort(); flat_vec}
TomBaston
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal_section = registry.entry(section.into()).or_insert(Vec::new()); let animal_string = animal.into(); if !animal_section.contains(&animal_string) { animal_section.push(animal_string); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result = registry.get(section).cloned().unwrap_or_default(); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = Vec::new(); for (_, value) in registry { for animal in value { result.push(animal.clone()); } } result.sort(); result}
elhananby
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_owned()).or_insert_with(Vec::new); if !animals.iter().any(|x| x == animal) { animals.push(animal.to_owned()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry .get(section) .map(|v| { let mut animals = v.clone(); animals.sort(); animals }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry .values() .flat_map(|v| v.iter().cloned()) .collect(); animals.sort(); animals}
elhananby
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_owned()).or_insert_with(Vec::new); if !animals.iter().any(|x| x == animal) { animals.push(animal.to_owned()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry .get(section) .map(|v| { let mut animals = v.clone(); animals.sort(); animals }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry .values() .flat_map(|v| v.iter().cloned()) .collect(); animals.sort(); animals}
elhananby
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result: Vec<String> = registry.get(section).cloned().unwrap_or_default(); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result: Vec<String> = registry.values().flatten().cloned().collect(); result.sort(); result}
Sommos
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let section = registry.entry(section.into()).or_default(); let animal = animal.to_owned(); if !section.contains(&animal) { section.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).cloned().unwrap_or_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<_> = registry.iter().map(|(_key, value)| value).flatten().cloned().collect(); animals.sort(); animals}
qcabanes-hobby
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { if let Some(animals) = registry.get(section) { if animals.iter().any(|a| a == animal) { return; } } registry.entry(section.to_string()).and_modify(|r| r.push(animal.to_string())).or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut cloned_animals = animals.clone(); cloned_animals.sort(); cloned_animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = registry.values().flat_map(|a| a.iter()).cloned().collect::<Vec<String>>(); animals.sort(); animals}
qcabanes-hobby
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { if let Some(animals) = registry.get(section) { if animals.iter().any(|a| a == animal) { return; } } registry.entry(section.to_string()).and_modify(|r| r.push(animal.to_string())).or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut cloned_animals = animals.clone(); cloned_animals.sort(); cloned_animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = registry.values().flat_map(|a| a.iter()).cloned().collect::<Vec<String>>(); animals.sort(); animals}
aleksanderkrauze
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let section = registry.entry(section.into()).or_default(); let animal = animal.to_owned(); if !section.contains(&animal) { section.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).cloned().unwrap_or_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<_> = registry.iter().map(|(_key, value)| value).flatten().cloned().collect(); animals.sort(); animals}
hilias
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry.entry(section.to_string()).and_modify(|animals| if !animals.contains(&animal.to_owned()) {animals.push(animal.to_string())}).or_insert(Vec::from([animal.to_string()]));}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut resp = match registry.get(section) { Some(animals) => animals.to_vec(), None => Vec::new(), }; resp.sort(); resp}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut seen = HashMap::new(); for animals in registry.values() { for animal in animals { seen.insert(animal, ()); } } let mut resp = seen.keys().map(|a| a.to_string()).collect::<Vec<String>>(); resp.sort(); resp}
vineelkovvuri
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let entry = registry .entry(section.to_string()) .or_insert(Vec::<String>::new()); if !entry.contains(&animal.to_string()) { entry.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut res = registry .get(§ion.to_string()) .unwrap_or(&Vec::<String>::new()) .clone(); res.sort(); res}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res: Vec<String> = registry.values().flat_map(|v| v.iter()).cloned().collect(); res.sort(); res}
TaiPoole
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(animals) => { let mut animals = animals.clone(); animals.sort(); animals } None => Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = vec![]; for (_, value) in registry { animals.extend_from_slice(&value); } animals.sort(); animals}
swandyr
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry .entry(section.to_string()) .and_modify(|section| { let animal = animal.to_string(); if !section.contains(&animal) { section.push(animal.to_string()) } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.get(section).cloned().unwrap_or(vec![]); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.values().cloned().flatten().collect::<Vec<String>>(); animals.sort(); animals}
SRVng
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let key = section.to_string(); let value = animal.to_string(); registry.entry(key).and_modify(|e| { if !e.contains(&value) { e.push(value.clone()); } }).or_insert(vec![value]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(s) = registry.get(section) { let mut _s = s.clone(); _s.sort(); _s } else { vec![] as Vec<String> }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = registry.iter().fold(vec![] as Vec<String>, |mut acc, (_, a)| { acc.append(&mut a.clone()); acc }); result.sort(); result}
jenny07007
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .or_insert(Vec::new()) .retain(|existing_animal| existing_animal != animal); registry.entry(section.to_string()) .or_insert(Vec::new()) .push(animal.to_string());}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(animals) => { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry .values() .flat_map(|a| a.clone()) .collect(); all_animals.sort(); all_animals}
HeavyMetalGeek
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_owned()) .and_modify(|sec| { if !sec.contains(&animal.to_owned()) { sec.push(animal.to_owned()) } }) .or_insert(vec![animal.to_owned()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(sec) = registry.get(section) { let mut sec_cpy = sec.clone(); sec_cpy.sort(); sec_cpy } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all: Vec<String> = registry.iter().flat_map(|(_, v)| v.clone()).collect(); all.sort(); all}
Herrgrim0
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let reg = registry.entry(section.to_string()).or_insert(vec![animal.to_string()]); reg.push(animal.to_string()); match registry.get_mut(section) { Some(reg) => reg.dedup(), _ => {}, };}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => {let mut v = animals.clone();v.sort(); return v}, _ => return vec![], };}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = vec![]; for val in registry.values() { animals.append(&mut val.clone()); }; animals.sort(); return animals;}
A-ndrey
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal = animal.to_string(); let v = registry.entry(section.to_string()).or_insert(vec![]); if !v.contains(&animal) { v.push(animal); };}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut v = match registry.get(§ion.to_string()) { Some(v) => v.to_vec(), None => vec![] }; v.sort_by_key(|a| a.to_lowercase()); v}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut v = vec![]; for val in registry.values() { v.extend_from_slice(&val); } v.sort_by(|a,b| a.to_lowercase().cmp(&b.to_lowercase())); v}
leemars
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let entry = registry.entry(section.to_string()).or_insert(vec![]); if !(*entry).contains(&animal.to_string()) { (*entry).push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut r = registry.get(§ion.to_string()).unwrap_or(&vec![]).clone(); r.sort(); r}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut r: Vec<_> = registry.values().flatten().cloned().collect(); r.sort(); r}
suraboy
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function registry.get(section).map_or_else(Vec::new, |animals| { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals })}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<_> = registry.values().flatten().cloned().collect(); all.sort(); all}
flakelolz
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).unwrap_or(&vec![]).to_vec(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = registry .values() .flatten() .cloned() .collect::<Vec<String>>(); animals.sort(); animals}
Smolyan91
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry.entry(section.to_string()) .and_modify(|animals| { let animal_str = animal.to_string(); if !animals.contains(&animal_str) { animals.push(animal_str) } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut cloned = animals.clone(); cloned.sort(); cloned.to_vec() } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = vec![]; for (_, animals) in registry.iter() { result.extend(animals.iter().cloned()) } result.sort(); result}
1eldiego
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let animal_string = animal.to_string(); registry .entry(section.to_string()) .and_modify(|sec| { if let None = sec.iter().find(|a| a == &&animal_string) { sec.push(animal_string.clone()); } }) .or_insert(vec![animal_string]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut own_animals = animals.to_vec(); own_animals.sort(); own_animals }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<_> = registry.values().flatten().cloned().collect(); all.sort(); all}
1eldiego
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry .entry(section.to_string()) .and_modify(|sec| { if !sec.contains(&animal.to_string()) { sec.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut own_animals = animals.to_vec(); own_animals.sort(); own_animals }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<_> = registry.values().flatten().cloned().collect(); all.sort(); all}
1eldiego
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry .entry(section.to_string()) .and_modify(|sec| { if let None = sec.iter().find(|a| a == &&animal.to_string()) { sec.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut own_animals = animals.to_vec(); own_animals.sort(); own_animals }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<_> = registry.values().flatten().cloned().collect(); all.sort(); all}
joebrokeit
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let addanim = registry .entry(section .to_string()) .or_insert(Vec::new()); if !addanim.contains(&animal.to_string()){ addanim.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section){ Some(anims)=>{ let mut sorted_anims = anims.clone(); sorted_anims.sort(); sorted_anims }, None => Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut anims:Vec<String>=Vec::new(); for (cate,val) in registry{ let anim = val.clone(); anims.extend(anim); } anims.sort(); anims}