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 Vec<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_sorted
: This function should return a copy of the entire registry with all animals sorted alphabetically in each section.
let mut registry = Collection::new();
add_animal_to_section("Eagle", "Birds", &mut registry);
assert_eq!(get_animals_in_section("Birds", ®istry), vec!["Eagle"]);
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.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(§ion.to_string()){ Some(animal) => { let mut sorted_animals = animal.clone(); sorted_animals.sort(); sorted_animals } None => Vec::new()} }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_sorted = registry.values().flat_map(|animals| animals.iter().cloned()).collect::<Vec<String>>(); all_sorted.sort(); all_sorted}
use std::{collections::HashMap, vec};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_with(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_vec) => { let mut animals = animals_vec.clone(); animals.sort(); animals }, None => { Vec::new() } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = Vec::new(); for (k, v) in registry { all_animals.extend(v.iter().cloned()); } all_animals.sort(); return all_animals;}
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(section) = registry.get_mut(§ion.to_string()) { if !section.contains(&animal.to_string()) { section.push(animal.to_string()) } } else { registry.insert(section.to_string(), vec!(animal.to_string())); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(value) = registry.get(§ion.to_string()) { let mut animals = value.to_vec(); animals.sort(); return animals } else { return Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = registry.values().flatten().cloned().collect::<Vec<String>>(); animals.sort(); animals}
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 animals = registry.entry(section.to_string()).or_insert_with(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> { // TODO: implement this function let section_vector: Vec<String> = Vec::new(); let animals = registry.get(section).unwrap_or(§ion_vector); let mut result = animals.clone(); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<String> = Vec::new(); for (_, value) in registry { for v in value { result.push(v.clone()); } } result.sort(); result}
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_vector: Vec<String> = Vec::new(); let animals = registry.entry(section.to_string()).or_insert(section_vector); if !(animals.contains(&animal.to_string())) { animals.push(animal.to_string()) } }pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let section_vector: Vec<String> = Vec::new(); let animals = registry.get(section).unwrap_or(§ion_vector); let mut result = animals.clone(); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<String> = Vec::new(); for (_, value) in registry { for v in value { result.push(v.clone()); } } result.sort(); result}
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 animals = registry.entry(section.to_string()).or_insert_with(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> { // TODO: implement this function let mut my_vec = registry.get(section).unwrap_or(&Vec::new()).to_vec(); my_vec.sort(); my_vec}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 }
use std::collections::HashMap;use std::vec;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![animal.to_string()]); let mut check: bool = false; for a in &*entry { if *a == animal { check = true; } } if !check { (*entry).push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let entry = registry.get(section); if entry != None { let mut ans = entry.unwrap().to_vec(); ans.sort(); ans } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut ans = Vec::<String>::new(); for (_, val) in registry.iter() { ans = [ans, val.to_vec()].concat(); } ans.sort(); ans}
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> { registry .get(section) .map(|animals| { let mut sorted = animals.clone(); sorted.sort(); sorted }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry .values() .flat_map(|animals| animals.iter()) .cloned() .collect(); all_animals.sort(); all_animals}
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::new()); if !reg.contains(&animal.to_string()){ reg.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut my_vec = registry.get(section).unwrap_or(&Vec::new()).to_vec(); my_vec.sort(); my_vec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut new_registry: Vec<String> = registry .values() .flat_map(|e| e.iter().cloned()) .collect(); new_registry.sort(); // sort a-z new_registry}
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(section) = registry.get_mut(§ion.to_string()) { if !section.contains(&animal.to_string()) { section.push(animal.to_string()) } } else { registry.insert(section.to_string(), vec!(animal.to_string())); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { //if let Some(animals) = registry.get(§ion.to_string()).unwrap().to_vec() { if let Some(value) = registry.get(§ion.to_string()) { let mut animals = value.to_vec(); animals.sort(); return animals } else { return Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = registry.values().flatten().cloned().collect::<Vec<String>>(); animals.sort(); animals}
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) { registry.insert(section.to_string(),vec![animal.to_string()]); } else { if !registry.get(section).unwrap().contains(&animal.to_string()) { registry.get_mut(section).unwrap().push(animal.to_string()); } }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v: Vec<String> = vec![]; for animal in registry.get(section).unwrap_or(&vec![]) { v.push(animal.to_string()); } v.sort(); v}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v = vec![]; for (_, animals) in registry { for animal in animals { v.push(animal.to_string()); } } v.sort(); v}
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) { registry.insert(section.to_string(),vec![animal.to_string()]); } else { if !registry.get(section).unwrap().contains(&animal.to_string()) { registry.get_mut(section).unwrap().push(animal.to_string()); } }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v: Vec<String> = vec![]; for animal in registry.get(section).unwrap_or(&vec![]) { v.push(animal.to_string()); } v.sort(); v}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v = vec![]; for (_, animals) in registry { for animal in animals { v.push(animal.to_string()); } } v.sort(); v}
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 = section.to_string(); if !registry.contains_key(§ion) { registry.insert(section.clone(), Vec::<String>::new()); } if !registry[§ion].contains(&animal.to_string()) { registry.get_mut(§ion).unwrap().push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let section = section.to_string(); if registry.contains_key(§ion) { let mut out = registry[§ion.to_string()].clone(); out.sort(); out } else { Vec::<String>::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut list : Vec<String> = Vec::new(); for (_, slist) in registry.iter() { for animal in slist.iter() { list.push(animal.clone()); } } list.sort(); list}
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_mut(section) { // NOTE: if get_mut returns Some(values), the values will be assigned to the animals. Additionally, get_mut return the MUTABLE REFERENCE to the value of the specified key. So, animals has &Vec<String> data type. if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } } else { registry.insert(section.to_string(), vec![animal.to_string()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { // NOTE: sort accepts a mutable reference argument so we need to clone the animals as mutable by using clone method and mut keyword. let mut sorted_animals = animals.clone(); sorted_animals.sort(); return sorted_animals; } Vec::new()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::new(); // NOTE: as we need to modify the vector, we need to create a mutable variable. for animals in registry.values() { all_animals.extend(animals.clone()); // NOTE: because the extend method moves the value so we need to use clone. } all_animals.sort(); all_animals}
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(); registry.entry(section.to_string()).and_modify(|animals| { if !animals.contains(&animal) { animals.push(animal.clone()) } }).or_insert(vec![animal]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(§ion.to_string()) { let mut animals = animals.clone(); animals.sort(); animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = Vec::new(); for (_, animals) in registry.iter() { all_animals.append(&mut animals.clone()); } all_animals.sort(); all_animals}
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 // Vérifier si la section existe déjà if let Some(animals) = registry.get_mut(section) { // Vérifie si l'animal est déjà dans la section if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } } else { // Si la section n'existe pas, la créer avec l'animal registry.insert(section.to_string(), vec![animal.to_string()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // Vérifie si la section existe if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); // Clone les animaux pour éviter d'emprunter mutablement sorted_animals.sort(); // Trie la liste des animaux return sorted_animals; } // Si la section n'existe pas, retourne une liste vide Vec::new()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = Vec::new(); for animals in registry.values() { //println!("{:?}", animals.1); all_animals.extend(animals.iter().cloned()); } // Trie la liste complète des animaux all_animals.sort(); all_animals}
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::<String>::new()); match registry[section].iter().find(|&item| item == animal) { Some(_) => (), None => registry.get_mut(section).unwrap().push(animal.to_string()) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut l = match registry.get(section) { None => Vec::<String>::new(), Some(list) => list.clone() }; l.sort(); l}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut l = Vec::<String>::new(); for section in registry.iter() { for animal in section.1.iter() { l.push(animal.clone()); } } l.sort(); l}
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 res : Vec<String> = vec!(); match registry.get(section) { Some(animals) => { res = animals.clone(); }, None => {} } res.sort(); res}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals : Vec<String> = vec!(); for (secton, animal) in registry { animals.extend(animal.clone()); } // TODO: implement this function animals.sort(); animals}
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!()); 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 res : Vec<String> = vec!(); match registry.get(section) { Some(animals) => { res = animals.clone(); }, None => {} } res.sort(); res}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals : Vec<String> = vec!(); for (secton, animal) in registry { animals.extend(animal.clone()); } // TODO: implement this function animals.sort(); animals}
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 mut animals = get_animals_in_section(section, registry); let mut should_push: bool = true; for a in animals.clone().into_iter() { if a == animal { should_push = false } } if should_push { animals.push(String::from(animal)); } registry.insert(String::from(section), animals);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut a = animals.clone(); a.sort(); a }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = Vec::new(); for (_key, value) in registry { for animal in value { if !all_animals.contains(animal) { all_animals.push(animal.clone()) } } } all_animals.sort_by_key(|w| w.to_lowercase()); all_animals}
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 mut animals = get_animals_in_section(section, registry); let mut should_push: bool = true; for a in animals.clone().into_iter() { if a == animal { should_push = false } } if should_push { animals.push(String::from(animal)); } registry.insert(String::from(section), animals);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut a = animals.clone(); a.sort(); a }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = Vec::new(); for (_key, value) in registry { for animal in value { if !all_animals.contains(animal) { all_animals.push(animal.clone()) } } } all_animals.sort_by_key(|w| w.to_lowercase()); all_animals}
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_with(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(sec) => { let mut animals = sec.clone().to_vec(); animals.sort(); animals }, None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = Vec::new(); for (_key, val) in registry.into_iter() { for animal in val { animals.push(animal.to_string()) } } animals.sort(); animals}
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.into()).or_insert_with(Vec::new); if !animals.contains(&animal.to_string()) { animals.push(animal.into()); }}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<_> = registry.values().flat_map(|v| v.iter()).cloned().collect(); animals.sort(); animals}
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_with(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> { // TODO: implement this function match registry.get(section) { Some(value) => { let mut mine = value.clone(); mine.sort(); mine }, None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut resultvec = Vec::new(); for (key, value) in registry.into_iter() { for animal in value { resultvec.push(animal.to_string()); } } resultvec.sort(); resultvec}
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(col) = registry.get_mut(section) { if !col.contains(&animal.to_string()) { col.push(animal.to_string()); } } else { let mut vec = Vec::new(); vec.push(animal.to_string()); registry.insert(section.to_string(), vec); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(col) = registry.get(section) { let mut result = col.to_vec(); result.sort(); return result; } return Vec::new();}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result: Vec<String> = registry .values() .flat_map(|animals| animals.iter().cloned()) .collect(); result.sort(); result}
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 // // 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. if ! registry.entry(section.to_string()).or_default().contains(&animal.to_string()) { registry .entry(section.to_string()) .or_default() .push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function // // 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. // let mut animals = registry.entry(section.to_string()).or_default().to_vec(); let mut animals = registry.get(section) .map(|v| v.to_vec()) .unwrap_or_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function // // This function should return a copy of the entire registry with all animals sorted alphabetically in each section. let mut all_animals: Vec<String> = Vec::new(); for animals in registry.values() { all_animals.extend(animals.clone()); } all_animals.sort(); all_animals.dedup(); all_animals}
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_owned()).and_modify(|v|{ if !v.contains(&animal.to_owned()) { v.push(animal.to_owned()); } }).or_insert(vec![animal.to_owned()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function if let Some(v) = registry.get(section) { let mut _v=v.clone(); _v.sort(); _v }else{ vec![] } }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut res = vec![]; for (_, v) in registry { for e in v.iter() { res.push(e.clone()); } } res.sort(); return res;}
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(|values| { if !values.contains(&animal.to_string()) { values.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(values) => { let mut sv = values.clone(); sv.sort(); return sv }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = vec![]; for (_, animals) in registry { all_animals.extend(animals.clone()); } all_animals.sort(); return all_animals;}
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(|values| { if !values.contains(&animal.to_string()) { values.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(values) => { let mut sv = values.clone(); sv.sort(); return sv }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = vec![]; for (_, animals) in registry { all_animals.extend(animals.clone()); } all_animals.sort(); return all_animals;}
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 // TODO: implement this function registry .entry(section.to_string()) .and_modify(|values| { // Проверяем, содержится ли animal уже в векторе if !values.contains(&animal.to_string()) { values.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(values) => { let mut sv = values.clone(); sv.sort(); return sv }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function // TODO: implement this function let mut all_animals = vec![]; for (_, animals) in registry { all_animals.extend(animals.clone()); } all_animals.sort(); return all_animals;}
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 sec = registry.entry(section.to_string()).or_insert(vec![]); if !sec.contains(&animal.to_string()){ sec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let result = registry.get(section); match result { Some(sec) => { let mut sorted = sec.to_vec(); sorted.sort(); sorted } _ => { vec![] } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut res = vec![]; for sec in registry.values() { let temp_sec = sec.clone(); res.extend(temp_sec); } res.sort(); res}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let a = animal.to_owned(); registry.entry(section.to_owned()) .and_modify(move |e| { if !e.contains(&a) { e.push(a); } }) .or_insert_with(|| vec![animal.to_owned()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(v) = registry.get(section) { let mut result = v.clone(); result.sort(); result } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = vec![]; for animals in registry.values() { result.extend(animals.into_iter().cloned()); } result.sort(); result}
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 sec = registry.entry(section.to_string()).or_insert(vec![]); if !sec.contains(&animal.to_string()){ sec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let result = registry.get(section); match result { Some(sec) => { let mut sorted = sec.to_vec(); sorted.sort(); sorted } _ => { vec![] } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut res = vec![]; for sec in registry.values() { let temp_sec = sec.clone(); res.extend(temp_sec); } res.sort(); res}
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) = (animal.to_string(), section.to_string()); if !registry.get(§ion).map(|vec| vec.contains(&animal)).unwrap_or(false) { registry.entry(section) .or_insert(Vec::new()) .push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(vec) => { let mut animals = vec.to_vec(); animals.sort(); animals }, None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = Vec::new(); registry.iter().for_each(|(_, vals)| { animals.extend(vals.to_vec()) }); animals.sort(); animals}
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) = (animal.to_string(), section.to_string()); let entry = registry.entry(section).or_default(); let has_animal = entry.contains(&animal); if !has_animal { entry.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let section = section.to_string(); match registry.get(§ion) { Some(animals) => { let mut animals = animals.to_vec(); animals.sort(); animals }, _ => {vec![]} }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().cloned().flatten().collect(); animals.sort(); animals}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let sec = registry.entry(section.to_string()).or_insert(vec![]); if !sec.contains(&animal.to_string()) { sec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let result = registry.get(section); match result { Some(sec) => { let mut sorted = sec.to_vec(); sorted.sort(); sorted } None => { Vec::new() } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut zoo = Vec::new(); for sec in registry.values() { zoo.extend_from_slice(sec); } zoo.sort(); zoo}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let sec = registry.entry(section.to_string()).or_insert(vec![]); if !sec.contains(&animal.to_string()) { sec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let result = registry.get(section); match result { Some(sec) => { let mut sorted = sec.to_vec(); sorted.sort(); sorted } None => { Vec::new() } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut zoo = Vec::new(); for sec in registry.values() { let temp_sec = sec.clone(); zoo.extend(temp_sec); } zoo.sort(); zoo}
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_vec = registry.entry(String::from(section)).or_insert(Vec::new()); if !section_vec.contains(&String::from(animal)) { section_vec.push(String::from(animal)); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(&String::from(section)) { Some(sec) => { let mut sorted = sec.to_vec(); sorted.sort(); sorted } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut zoo = Vec::new(); for sec in registry.values() { let temp_sec = sec.clone(); zoo.extend(temp_sec); } zoo.sort(); zoo}
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_vec = registry.entry(String::from(section)).or_insert(Vec::new()); if !section_vec.contains(&String::from(animal)) { section_vec.push(String::from(animal)); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(&String::from(section)) { Some(sec) => { let mut sorted = sec.to_vec(); sorted.sort(); sorted } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut zoo = Vec::new(); for sec in registry.values() { let temp_sec = sec.clone(); zoo.extend(temp_sec); } zoo.sort(); zoo}
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 list :&mut Vec<String> = registry.get_mut(section).unwrap(); if !list.contains(&animal.to_string()) { list.push(animal.to_string()) } } else { let list : Vec<String> = Vec::from([animal.to_string()]); registry.insert(section.to_string(), list); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(val) => { let mut list = val.clone(); list.sort(); list.to_vec() }, None => vec![] } }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut sorted: Vec<String> = Vec::new(); for (_, val) in registry.iter() { let mut list = val.clone(); list.sort(); sorted.extend(list); } sorted.sort(); sorted}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let current_animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !current_animals.contains(&animal.to_string()) { current_animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let entries = registry.get(section); match entries { Some(section) => { let mut list = section.to_vec(); list.sort(); return list; }, None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut sorted = Vec::new(); for (_, v) in registry.iter() { for a in v { sorted.push(String::from(a)); } } sorted.sort(); sorted}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let new_animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !new_animals.contains(&animal.to_string()) { new_animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let entries = registry.get(section); match entries { Some(section) => { let mut list = section.to_vec(); list.sort(); return list; } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut sorted = Vec::new(); for (_, v) in registry.iter() { for a in v { sorted.push(String::from(a)); } } sorted.sort(); sorted}
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![]); if !v.contains(&animal.to_string()) { v.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(list) => { let mut list = list.to_vec(); list.sort(); return list }, None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = Vec::new(); for (_, animals) in registry.iter() { let animals = animals.to_vec(); animals.into_iter().for_each(|a| all_animals.push(a)); } all_animals.sort(); all_animals }
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![]); if !v.contains(&animal.to_string()) { v.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(list) => { let mut list = list.to_vec(); list.sort(); return list }, None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = Vec::new(); for (_, animals) in registry.iter() { let animals = animals.to_vec(); animals.into_iter().for_each(|a| all_animals.push(a)); } all_animals.sort(); all_animals }
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 register = registry.entry(section.to_string()).or_insert(vec![]); if !register.contains(&animal.to_string()) { register.push(animal.to_string()) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(animals) => { let mut result = animals.clone(); result.sort(); result.to_vec() } None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<String> = vec![]; for (_, animals) in registry.iter() { for animal in animals { result.push(animal.to_string()); } } result.sort(); result}
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![]); if !v.contains(&animal.to_string()) { v.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(list) => { let mut list = list.to_vec(); list.sort(); return list }, None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = Vec::new(); for (_, animals) in registry.iter() { let animals = animals.to_vec(); animals.into_iter().for_each(|a| all_animals.push(a)); } all_animals.sort(); all_animals }
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.into()).or_default(); let animal = animal.to_string(); if !animals.contains(&animal) { animals.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result = registry.get(section).cloned().unwrap_or_default(); result.sort_unstable(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result: Vec<_> = registry.values().flatten().cloned().collect(); result.sort_unstable(); result}
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 match registry.get_mut(section) { Some(section) => { if !section.contains(&String::from(animal)) { section.push(animal.to_string()); } }, None => { 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 if let Some(section) = registry.get(section) { let mut sorted = section.to_vec(); sorted.sort(); return sorted; } else { return Vec::<String>::new(); }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut list_of_animals = Vec::<String>::new(); for (_,animals) in registry { list_of_animals.extend(animals.to_vec()) } list_of_animals.sort(); return list_of_animals;}
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_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = Vec::new(); for animals in registry.values() { let mut cloned_animals = animals.clone(); cloned_animals.sort(); all_animals.extend(cloned_animals); } all_animals.sort(); all_animals // let mut all_animals: Vec<String> = registry.values().flat_map(|animals| animals.iter().cloned()).collect(); // all_animals.sort(); // all_animals}
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.into()).or_default(); let animal = animal.to_string(); if !animals.contains(&animal) { animals.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result = registry.get(section).cloned().unwrap_or_default(); result.sort_unstable(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result: Vec<_> = registry.values().flatten().cloned().collect(); result.sort_unstable(); result}