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) { // 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 vc: Vec<String>; vc = registry.get(section).cloned().unwrap_or_default(); vc.sort(); vc }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<String> = Vec::new(); for (_, value) in registry.iter() { all.extend(value.clone()); } all.sort(); all}
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 vc: Vec<String>; vc = registry.get(section).cloned().unwrap_or_default(); vc.sort(); vc }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<String> = Vec::new(); for (_, value) in registry.iter() { all.extend(value.clone()); } all.sort(); all}
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> { // TODO: implement this function let mut animals = registry.get(section).unwrap_or(&Vec::new()).clone(); animals.sort(); animals}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;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> { 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<String> = registry .values() .flatten() .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> { 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<String> = registry .values() .flatten() .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) { // Get or create the section's animal list let animals = registry.entry(section.to_string()).or_insert(Vec::new()); // Add animal only if it's not already present if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // Get the animal list for the section, or an empty vector if section doesn't exist registry.get(section).map_or(Vec::new(), |animals| { // Return a sorted copy of the animal list let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals })}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // Collect all animals from all sections into a single vector let mut all_animals: Vec<String> = registry .values() .flat_map(|animals| animals.clone()) .collect(); // Sort the combined list alphabetically 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) { // Get or create the section's animal list let animals = registry.entry(section.to_string()).or_insert(Vec::new()); // Add animal only if it's not already present if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // Get the animal list for the section, or an empty vector if section doesn't exist registry.get(section).map_or(Vec::new(), |animals| { // Return a sorted copy of the animal list let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals })}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // Collect all animals from all sections into a single vector let mut all_animals: Vec<String> = registry .values() .flat_map(|animals| animals.clone()) .collect(); // Sort the combined list alphabetically 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(String::from(section)) .and_modify(|animals| if !animals.contains(&String::from(animal)) {animals.push(String::from(animal))} ) .or_insert(vec!(String::from(animal))); // TODO: implement this function}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let empty:Vec<String> = vec!(); let mut animals = registry.get(section).map(|v| v.clone()).unwrap_or(empty); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = registry.values() .flat_map(|v| v.clone()) .collect(); all_animals.sort(); all_animals}
use std::collections::HashMap;use std::collections::HashSet;use std::iter::FromIterator;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 if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let all_animals: Vec<String> = registry.values().flatten().cloned().collect(); let unique_animals: HashSet<String> = HashSet::from_iter(all_animals); let mut animals_vec = Vec::from_iter(unique_animals); animals_vec.sort(); animals_vec}
use std::collections::HashMap;use std::collections::HashSet;use std::iter::FromIterator;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 if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let all_animals: Vec<String> = registry.values().flatten().cloned().collect(); let unique_animals: HashSet<String> = HashSet::from_iter(all_animals); let mut animals_vec = Vec::from_iter(unique_animals); animals_vec.sort(); animals_vec}
use std::collections::HashMap;// section name -> vec of animalstype Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { println!("add {animal} {section}"); registry.entry(section.to_string()) .and_modify(|v| if !v.contains(&animal.to_string()) { v.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 = match registry.get(section) { Some(val) => val.to_vec(), None => vec![] }; animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = vec![]; for arr in registry.values() { for animal in arr { if !animals.contains(animal) { 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) { registry .entry(section.to_string()) .and_modify(|x| if !x.contains(&(animal.to_string())) {x.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 = registry.values().flat_map(|x| x.clone()).collect::<Vec<String>>(); animals.sort(); animals}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;/// Menambahkan hewan ke section, hindari duplikatpub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal = animal.to_string(); // Ambil vektor hewan berdasarkan section, atau buat baru let section_animals = registry.entry(section.to_string()).or_insert_with(Vec::new); // Tambahkan hanya jika belum ada if !section_animals.contains(&animal) { section_animals.push(animal); } }/// Mengambil daftar hewan dari suatu section, diurutkan alfabetpub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry .get(section) .cloned() .unwrap_or_default(); // Urutkan secara alfabet (case-insensitive) animals.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = Vec::new(); for animal_list in registry.values() { animals.extend(animal_list.clone()); } // Case-insensitive alphabetical sort (A-Z) animals.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); 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 entry = registry .entry(section.to_string()) .or_insert(vec![animal.to_string()]); 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 animals = match registry.get(section) { Some(animals) => animals.clone(), None => Vec::new(), }; animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<String> = Vec::new(); for (key, value) in registry.iter() { all.extend(value.clone()); } all.sort(); all}
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(vec1) = registry.get_mut(section) { if !vec1.contains(& animal.to_string()){ vec1.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(vec1) = registry.get(section){ let mut outvec = vec1.to_vec(); outvec.sort(); outvec } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut outvec = Vec::new(); for animals in registry.values() { let mut intermed = animals.clone(); outvec.append(&mut intermed); } outvec.sort(); outvec}
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_default(); if !animals.iter().any(|a| a == animal) { animals.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_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry .values() .flat_map(|animal| animal.clone()) .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) { // TODO: implement this function registry.entry(section.to_string()) .and_modify(|vector| { if ! vector.contains(&animal.to_string()) { vector.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 key = registry.get(§ion.to_string()); let vector = match key { Some(vec_str) => { let mut v = vec_str.clone(); v.sort(); v}, None => Vec::<String>::new() }; vector}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_vec: Vec<String> = Vec::new(); for (key, value) in registry.iter() { all_vec.extend_from_slice(&value); } all_vec.sort(); all_vec}/* Iteration zero:registry = []First iteration:registry = ["Birds" => vec!["Eagle"]]Second iteration:registry = ["Birds" => vec!["Eagle", "Pigeon"]]*/
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 if let Some(animals) = registry.get(section) { let mut animals_sorted = animals.clone(); animals_sorted.sort(); animals_sorted } else { 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() { all_animals.extend(animals.iter().cloned()); } 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.to_string()).or_default(); if !animals.iter().any(|a| a == animal) { 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 animals: Vec<String> = registry .values() .flat_map(|animal| animal.clone()) .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) { // 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 if let Some(animals) = registry.get(section) { let mut animals_sorted = animals.clone(); animals_sorted.sort(); animals_sorted } else { 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() { all_animals.extend(animals.iter().cloned()); } 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 match registry.get_mut(section) { Some(t) => { if !(*t).contains(&animal.to_string()){ (*t).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 return match registry.get(section){ Some(t) => { let mut ret = (*t).clone(); ret.sort(); ret }, None => vec![], };}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut res = vec![]; for (_,v) in registry.iter() { res.extend_from_slice(&v); } 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) { // 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_with(|| vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function 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> { // TODO: implement this function let mut all_animals: Vec<String> = registry.values().flatten().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 registry.entry(section.to_string()) .or_insert_with(Vec::new);// .push(animal.to_string()); let vec= registry.get_mut(section).unwrap(); if vec.iter().any(|elem| elem == &animal.to_string()) { return; } vec.push(animal.to_string());}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut outvec = registry.get(section).unwrap_or(&vec![]).to_vec(); outvec.sort(); outvec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animal_vec: Vec<String> = vec![]; for (key, _val) in registry.iter(){ let b = get_animals_in_section(&key, registry);// b.sort(); animal_vec.extend(b); } animal_vec.sort(); animal_vec}
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()) .or_insert_with(Vec::new);// .push(animal.to_string()); let vec= registry.get_mut(section).unwrap(); if vec.iter().any(|elem| elem == &animal.to_string()) { return; } vec.push(animal.to_string());}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut outvec = registry.get(section).unwrap_or(&vec![]).to_vec(); outvec.sort(); outvec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animal_vec: Vec<String> = vec![]; for (key, _val) in registry.iter(){ let b = get_animals_in_section(&key, registry);// b.sort(); animal_vec.extend(b); } animal_vec.sort(); animal_vec}
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()) .or_insert_with(Vec::new);// .push(animal.to_string()); let vec= registry.get_mut(section).unwrap(); if !vec.iter().any(|elem| elem == &animal.to_string()) { vec.push(animal.to_string()); } }pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut outvec = registry.get(section).unwrap_or(&vec![]).to_vec(); outvec.sort(); outvec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animal_vec: Vec<String> = vec![]; for (key, _val) in registry.iter(){ let b = get_animals_in_section(&key, registry);// b.sort(); animal_vec.extend(b); } animal_vec.sort(); animal_vec}
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) { if !registry.contains_key(section) { registry.insert(section.to_string(), Vec::new()); } let section_data = registry.get_mut(section).expect("registry invariant violation"); let animal_str = animal.to_string(); if !section_data.contains(&animal_str) { section_data.push(animal_str); } // TODO: implement this function}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some (section_data) => { let mut animals = section_data.to_vec(); animals.sort(); animals } None => Vec::new() } // TODO: implement this function}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = HashSet::new(); for (_, section_data) in registry.iter() { for animal in section_data.iter() { all_animals.insert(animal.clone()); } } let mut result = all_animals.into_iter().collect::<Vec<_>>(); result.sort(); result // TODO: implement this function}
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 if let Some(animals) = registry.get(section) { let mut animals_sorted = animals.clone(); animals_sorted.sort(); animals_sorted } else { 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() { all_animals.extend(animals.iter().cloned()); } 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 vec = registry.entry(section.to_owned()).or_default(); let str_animal = animal.to_owned(); if !vec.contains(&str_animal) { vec.push(str_animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut vec = registry.get(section).cloned().unwrap_or_default(); vec.sort(); // Sorts alphabetically vec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<String> = Vec::new(); for (_, val) in registry { all.extend(val.clone()); } all.sort(); all}
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_in_section = registry.entry(section.to_owned()).or_default(); if !animals_in_section.contains(&animal.to_owned()) { animals_in_section.push(animal.to_owned()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = match registry.get(section) { Some(animal_set) => animal_set.iter().cloned().collect(), None => Vec::new(), }; animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flatten().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) { match registry.get_mut(section) { Some(animals) => { if !animals.contains(&animal.to_string()){ animals.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> { let section_exists = registry.get(&String::from(section)); match section_exists { Some(animals) => { let mut animals_by_section = (*animals).to_vec(); animals_by_section.sort(); animals_by_section }, None => { Vec::new() }, }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut sorted_animals : Vec<String> = Vec::new(); for (_key, section) in registry { for animal in section.iter() { sorted_animals.push(animal.clone()); } } sorted_animals.sort(); sorted_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 match registry.get_mut(section) { Some(animal_vec) => { // animal_vec.push(animal.to_string()); // animal_vec.dedup(); if !animal_vec.contains(&animal.to_string()) { animal_vec.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 match registry.get(section) { Some(e) => { let mut dummy = e.to_vec(); dummy.sort(); return dummy; }, None => 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 (_, v) in registry.iter() { all_animals.extend_from_slice(v); } 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 match registry.get_mut(section) { Some(val) => { if !val.contains(&animal.to_string()) { val.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 match registry.get(section) { Some(val) => { let mut val_cp = val.to_vec(); val_cp.sort(); val_cp.to_vec() }, None => Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animal: Vec<String> = Vec::new(); for(_, v) in registry.iter() { all_animal.extend_from_slice(v); } all_animal.sort(); all_animal}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // Only add the animal if it's not already in the section 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> { // Return the list of animals in the section, or an empty list if the section doesn't exist. match registry.get(section) { Some(animals) => { let mut cloned_animals = animals.clone(); cloned_animals.sort(); // Sort the cloned vector before returning cloned_animals } None => Vec::new(), // Return an empty vector if the section is not found }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // Collect all animals from all sections into a single vector, sort it, and return it. let mut all_animals: Vec<String> = registry .values() .flat_map(|animals| animals.clone()) .collect(); all_animals.sort(); all_animals.dedup(); // Remove duplicates 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_string()).and_modify(|v| { if !v.contains(&animal.to_string()) { v.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 res =registry.get(section).unwrap_or(&Vec::new()).clone(); res.sort(); res.dedup(); res}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut collections = Vec::new(); for (section, animals) in registry.iter() { collections.extend_from_slice(animals) } collections.sort(); collections.dedup(); // collections.push("123".to_string()); collections}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;// "Felines": <"Tiger", "Cat", "Lion">// Workflow// 1)Check whether the key exists// 1.1) If it does, borrow the Vec as mutable and add the desired animal to it// 1.2) If it does NOT exist, make a new key: value pair in the desired formatpub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function //Skip the checking and get strait to modifying if let Some(anim_vector) = registry.get_mut(section){ anim_vector.push(animal.to_string()); //Sort and deduplicate //anim_vector.sort(); anim_vector.dedup(); } 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 let mut anim_vec = Vec::new(); if registry.contains_key(section) { for x in registry[section].iter(){ anim_vec.push(x.to_string()); } } anim_vec.sort(); anim_vec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut anim_vec = Vec::new(); for (_section, animal) in registry{ anim_vec.extend(animal.clone()); } anim_vec.sort(); anim_vec}
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> { 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 = vec![]; for a in registry.values() { let a = a.clone(); animals.extend(a); } 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(Vec::new()); let a = animal.to_string(); if !animals.contains(&a) { animals.push(a); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(§ion.to_string()) { None => vec![], Some(v) => { let mut result = v.to_vec(); result.sort(); result } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = vec![]; for (_, animals) in registry.iter() { for a in animals.iter() { if !result.contains(a) { result.push(a.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) { 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) => { 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_sorted_animals = Vec::new(); for (_, animals) in registry.iter() { let a = animals.clone(); all_sorted_animals.extend(a); } all_sorted_animals.sort(); all_sorted_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> { 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 = vec![]; for a in registry.values() { let a = a.clone(); animals.extend(a); } 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> { 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_sorted_animals = Vec::new(); for (_, animals) in registry.iter() { let a = animals.clone(); all_sorted_animals.extend(a); } all_sorted_animals.sort(); all_sorted_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(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_sorted_animals = Vec::new(); for (_, animals) in registry.iter() { let a = animals.clone(); all_sorted_animals.extend(a); } all_sorted_animals.sort(); all_sorted_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 section_entry = registry.entry(section.to_owned()).or_insert(Vec::new()); if !section_entry.contains(&animal.to_owned()){ section_entry.push(animal.to_owned()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let animals = registry.get(section); match animals { Some(animals_vec) => { let mut sorted = animals_vec.clone(); sorted.sort(); sorted } None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<String> = Vec::new(); for (_, val) in registry { all.extend(val.clone()); } all.sort(); all}
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) { if let Some(lst) = registry.get_mut(section) { if !lst.contains(&animal.to_string()) { lst.push(animal.to_string()); } } else { 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 registry .get(section) .map(|s| { let mut a = s.clone(); a.sort(); a }) .unwrap_or(Vec::new())}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all = HashSet::new(); for (_k, v) in registry.iter() { for l in v.iter() { all.insert(l.clone()); } } let mut ret: Vec<String> = all.into_iter().collect(); ret.sort_unstable(); ret}
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) { if let Some(lst) = registry.get_mut(section) { if !lst.contains(&animal.to_string()) { lst.push(animal.to_string()); } } else { 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 registry .get(section) .map(|s| { let mut a = s.clone(); a.sort(); a }) .unwrap_or(Vec::new())}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all = HashSet::new(); for (_k, v) in registry.iter() { for l in v.iter() { all.insert(l.clone()); } } let mut ret: Vec<String> = all.into_iter().collect(); ret.sort_unstable(); ret}
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_entry = registry.entry(String::from(section)).or_insert(Vec::new()); if !section_entry.contains(&String::from(animal)) { section_entry.push(String::from(animal)); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let animals = registry.get(section); match animals { Some(animal_vec) => { let mut sorted_animals = animal_vec.clone(); sorted_animals.sort(); sorted_animals } None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = Vec::new(); for (_, value) in registry { all_animals.extend(value.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 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) => { 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_sorted_animals = Vec::new(); for (_, animals) in registry.iter() { let a = animals.clone(); all_sorted_animals.extend(a); } all_sorted_animals.sort(); all_sorted_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 match registry.get_mut(section) { Some(vec) => { if !vec.contains(&animal.to_string()) { vec.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 match registry.get(section) { Some(val) => { let mut newvec = val.to_vec(); newvec.sort(); newvec } 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 { for animal in animals { all_animals.push(animal.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 if registry.contains_key(§ion.to_string()){ if registry.get(section).unwrap().contains(&animal.to_string()){ return; } registry.get_mut(§ion.to_string()).unwrap().push(animal.to_string()); return; } registry.insert(section.to_string(),vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if !registry.contains_key(§ion.to_string()){ return vec![]; } let mut copy = registry.get(§ion.to_string()).unwrap().clone(); copy.sort(); copy}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::new(); for animals in registry.values(){ for animal in animals.iter(){ all_animals.push(animal.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 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(vec) => { let mut vec_cloned = vec.clone(); vec_cloned.sort(); vec_cloned }, None => { Vec::new() } } }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vec = Vec::new(); for (key, value) in registry.iter() { for animal in value { vec.push(animal.clone()); } } vec.sort(); vec}
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::new()); if list.contains(&animal.to_string()) { return; } list.push(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 } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all: Vec<String> = Vec::new(); for (_, i) in registry.iter() { all.extend(i.iter().cloned()); } all.sort(); all}