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 a section does not exist, make a new vector for it 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> { //make a vector, then all the animals get added to it. //sort it afterwards. let mut all_anim = Vec::<String>::new(); for (_, animals) in registry.iter() { for animal in animals { all_anim.push(animal.clone()); } } all_anim.sort(); all_anim}
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 a section does not exist, make a new vector for it 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> { //make a vector, then all the animals get added to it. //sort it afterwards. let mut all_anim = Vec::<String>::new(); for (_, animals) in registry.iter() { for animal in animals { all_anim.push(animal.clone()); } } all_anim.sort(); all_anim}
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.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = Vec::new(); if let Some(animals_in_section) = registry.get(§ion.to_string()) { for animal in animals_in_section { animals.push(animal.to_string()); } } animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = Vec::new(); for animals_in_section in registry.values() { for animal in animals_in_section { 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.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> { if let Some(animals) = registry.get(section){ let mut vec = animals.clone(); vec.sort(); vec }else{ Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::new(); for e in registry.iter() { all_animals.extend_from_slice(e.1); } //animals.sort(); 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(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).cloned().unwrap_or_default(); 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(|animals| animals.clone()) .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) { // 使用 entry API 安全添加(不覆盖现有数据) let animal_list = registry .entry(section.to_string()) .or_insert_with(Vec::new); let animal_str = animal.to_string(); if !animal_list.contains(&animal_str) { animal_list.push(animal_str); } }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> = registry .values() .flat_map(|animals| animals.clone()) .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 &mut ani = registry.entry(section); //if ani.iter().find(animal).is_none() { // ani.push(animal) //} registry.entry(section.to_string()).and_modify( |v| { if v.iter().find( |&x| *x == animal.to_string()).is_none() { 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 res = registry.get(section).cloned().unwrap_or(vec![]);//.iter().collect::<Vec<String>>(); res.sort(); res}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // registry.iter().reduce(|v,w| v.append(w)).unwrap() // registry.iter().map(|(k,v)| v.clone()).collect::<Vec<String>>() let mut animals = Vec::new(); for (k,v) in registry{ let mut cv = v.clone(); cv.sort(); animals.append(&mut cv); } 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 section = registry.entry(section.to_string()).or_default(); 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 if !registry.contains_key(section) { return Vec::<String>::new() } let mut section_id = registry.get(section).unwrap().clone(); section_id.sort(); section_id}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals_list = Vec::<String>::new(); for (_, animals) in registry { for animal in animals { animals_list.push(animal.clone()); } } animals_list.sort(); animals_list}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let mut animals = get_animals_in_section(section,registry); if !animals.iter().any(|item| item == animal) { animals.push(animal.to_string()) } registry.insert(section.to_string(),animals);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section){ let mut clone_animals = animals.clone(); clone_animals.sort(); clone_animals } else { Vec::<String>::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut new_animals = Vec::<String>::new(); for (section,animals) in registry.iter(){ new_animals.extend_from_slice(animals); } new_animals.sort(); new_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 let Some(value) = registry.get_mut(section) { if !value.contains(&animal.to_string()) { value.push(animal.to_string()); } } else { let new_animals:Vec<String> = vec![animal.to_string()]; registry.insert(section.to_string(), new_animals); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function if let Some(value) = registry.get(section) { let mut animals:Vec<String> = value.clone(); animals.sort(); return animals } else { return vec![]; }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut new_animals: Vec<String> = vec![]; for (_, animals) in registry.iter() { new_animals.extend(animals.clone()); } new_animals.sort(); return new_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()); if !animals.contains(&animal.to_string()){ animals.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 v2 = animals.clone(); v2.sort(); v2 }else{ Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = Vec::new(); for e in registry.iter() { animals.extend_from_slice(e.1); } 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()); if !animals.contains(&animal.to_string()){ animals.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 v2 = animals.clone(); v2.sort(); v2 }else{ Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = Vec::new(); for e in registry.iter() { animals.extend_from_slice(e.1); } 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_in_section = registry.entry(section.to_string()).or_insert_with(Vec::new); if !animals_in_section.contains(&animal.to_string()) { animals_in_section.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 sorted_animals = animals.clone(); sorted_animals.sort(); sorted_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_section in registry.values() { for animal in animals_in_section { 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 s = registry.entry(section.to_string()).or_default(); if !s.contains(&animal.to_string()) { s.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.clone(); sorted.sort(); sorted }else { vec![] }}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.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.to_string()).or_insert(Vec::new()); let s = animal.to_string(); if !animals.contains(&s) { animals.push(s); }}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 = 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 s = registry.entry(section.to_string()).or_default(); if !s.contains(&animal.to_string()) { s.push(animal.to_string()); } }pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut s = registry.get(section).cloned().unwrap_or_default(); s.sort(); s}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all = registry.values().flatten().cloned().collect::<Vec<_>>(); 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_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 animals = registry.get(section).cloned().unwrap_or_default(); 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}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let s = registry.entry(section.to_string()).or_default(); if !s.contains(&animal.to_string()) { s.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut s = registry.get(section).cloned().unwrap_or_default(); s.sort(); s}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all = registry.values().flatten().cloned().collect::<Vec<_>>(); 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_default(); if !animals.contains(&animal.to_string()){ //animals.iter().find(|x|{ *x == animal}) == None { 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>::new(); //for value in registry.values().flatten() { // if animals.iter().find(|x|{*x == value}) == None { // animals.push(value.to_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) { // TODO: implement this function let animals = registry.entry(section.to_string()).or_default(); 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).cloned().unwrap_or_default(); 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().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) { registry.entry(section.to_string()) .or_insert_with(Vec::new) .iter() .find(|a| a == &&animal) .is_none() .then(|| registry.get_mut(section).unwrap().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_sorted = animals.clone(); animals_sorted.sort(); animals_sorted } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result: Vec<String> = Vec::new(); for element in registry.iter() { result.extend(element.1.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 mut animals = registry.entry(section.to_string()).or_insert(Vec::new()); let animal = animal.to_string(); if !animals.contains(&animal) { animals.push(animal) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut animals = animals.clone(); animals.sort(); animals } None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = registry.values().flat_map(|animals| animals.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 // (If the registry does not contain a key named section, then create that key-value pair with that section name as the key and an empty vector as the value) if !registry.contains_key(section) { registry.insert(section.to_string(), Vec::new()); } // .get_mut() gets the actual mutable reference to the vector in the HashMap where .unwrap() ensures that a value inside Some(val) is unwraped, but that in the case there is no value the program will panic let section_ref = registry.get_mut(section).unwrap(); if !section_ref.contains(&animal.to_string()) { // here is the logic required where if an animal isn't in a section vector then it is pushed onto that vector as a String (instead of an &str) as the HashMap is defined with values as a Vec of Strings section_ref.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function // .contains_key() is used so that in case a key (section) does not exist then it returns an empty Vec of Strings if !registry.contains_key(section) { return Vec::<String>::new(); } let mut section_ref = registry.get(section).unwrap().clone(); // sorts the section alphabetically section_ref.sort(); // returns the section - because remember that section_ref.sort() doesn't return anything section_ref}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function // creates a new mutable vector of Strings let mut animals_final = Vec::<String>::new(); // for (key, value) pair in registry (where _ is used because I don't care about the section), iterate through the String Vec in each section and push the animal into the /* for (_, animals) in registry { for animal in animals { animals_final.push(animal.clone()); } } */ for (_, animals) in registry { animals_final.extend(animals.iter().cloned()); } animals_final.sort(); animals_final}
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.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 = 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) { if let Some(section_chunk) = registry.get_mut(section) { let added_animal = animal.to_string(); if !section_chunk.contains(&added_animal) { section_chunk.push(added_animal); } } else { registry.insert(section.to_string(), vec![animal.to_string()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(animal) => { let mut a = animal.to_vec(); a.sort(); return a; } None => return Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut a = registry .clone() .into_values() .flatten() .collect::<Vec<String>>(); a.sort(); a}
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_default(); 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 registry.get(section).map( |list| { let mut list = list.clone(); list.sort(); list //cannot use return }).unwrap_or_default()}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 //cannot use return}
use std::collections::HashMap;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_ref = registry.get_mut(section).unwrap(); if !section_ref.contains(&animal.to_string()) { section_ref.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if !registry.contains_key(section) { return Vec::<String>::new() } let mut section_ref = registry.get(section).unwrap().clone(); section_ref.sort(); section_ref}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals_final = Vec::<String>::new(); for (_, animals) in registry { for animal in animals { animals_final.push(animal.clone()); } } animals_final.sort(); animals_final }
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); // Only add if the animal doesn't already exist in this section 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_else(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) { // TODO: implement this function let animals_at_section = registry.entry(section.to_string()).or_default(); if !animals_at_section.contains(&animal.to_string()) { animals_at_section.push(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 tmp_list = animals.clone(); tmp_list.sort(); tmp_list }).unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let all_animals = registry.values(); let mut all_animals_sorted = Vec::new(); for val in all_animals { for animal in val { all_animals_sorted.push(animal.to_string()); } } all_animals_sorted.sort(); return all_animals_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 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_in_section: Vec<String>; if let Some(animals) = registry.get(section) { animals_in_section = animals.clone(); animals_in_section.sort(); return animals_in_section; } 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_from_slice(animals); } 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 section = registry.entry(section.to_string()).or_default(); 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 registry.get(section).map( |list| { let mut list = list.clone(); list.sort(); list //cannot use return }).unwrap_or_default()}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 //cannot use return}
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_default(); if !animals.iter().any(|a| a == animal) { animals.push(animal.to_owned()); } // TODO: implement this function}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 // TODO: implement this function}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all: Vec<String> = registry .values() .flat_map(|animals| animals.iter()) .cloned() .collect(); println!("{:?}", all); all.sort(); println!("{:?}", all); all // 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) { let animals = registry .entry(section.to_owned()) .or_default(); if !animals.iter().any(|a| a == animal) { animals.push(animal.to_owned()); } // TODO: implement this function}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 // TODO: implement this function}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all: Vec<String> = registry .values() .flat_map(|animals| animals.iter()) .cloned() .collect(); println!("{:?}", all); all.sort(); println!("{:?}", all); all // 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) { let section = registry .entry(section.to_string()) .or_default(); if !section.contains(&animal.to_string()) { section.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry .get(section) .map( |list| { let mut list = list.clone(); list.sort(); list }) .unwrap_or_default()}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}
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.to_string()) .or_default(); if !section.contains(&animal.to_string()) { section.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry .get(section) .map( |list| { let mut list = list.clone(); list.sort(); list }) .unwrap_or_default()}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}
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.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).cloned().unwrap_or_default(); 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) { if registry.contains_key(§ion.to_string()) { let v: &mut Vec<String> = registry.get_mut(section).unwrap(); if !v.contains(&animal.to_string()) { v.push(animal.to_string()); } } else { registry.insert(section.to_string(), Vec::<String>::new()); add_animal_to_section(animal, section, registry); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let opt = registry.get(§ion.to_string()); match opt { Some(v) => { let mut owned: Vec<String> = v.to_vec(); owned.sort(); owned.to_vec() } None => Vec::<String>::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut v: Vec<String> = registry.clone().into_values().into_iter().flatten().collect(); 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 v = registry.entry(section.to_string()).or_insert(Vec::new()); let animal = animal.to_string(); if !v.contains(&animal) { v.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut v = registry.get(section).cloned().unwrap_or_default(); v.sort(); v}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut v: Vec<String> = registry.values().flatten().cloned().collect(); 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 let v = registry.entry(section.to_owned()).or_insert(Vec::new()); let animal = animal.to_owned(); 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: Vec<String> = registry.get(section).unwrap_or(&vec![]).iter().cloned().collect(); v.sort(); v}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v: Vec<String> = registry.values().flat_map(|v| { v.iter().cloned() }).collect(); 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 animals = registry.entry(section.to_string()).or_insert(vec![]); 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).unwrap_or(&vec![]).to_vec(); 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(value) => { let value_to_push = String::from(animal); if !value.contains(&value_to_push) { value.push(value_to_push); } }, None => { let key = String::from(section); let mut value = Vec::<String>::new(); value.push(String::from(animal)); registry.insert(key, value); }, };}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result = Vec::<String>::new(); match registry.get(section) { Some(values) => { result = values.clone(); result.sort(); }, None => {}, } result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = Vec::<String>::new(); for values in registry.values() { let mut animals = values.clone(); result.append(&mut animals); } 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 entry = registry.entry(section.to_string()).or_default(); let animal = animal.to_string(); if !entry.contains(&animal) { entry.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(sect) = registry.get(§ion.to_string()) { let mut res = sect.clone(); res.sort(); res } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res = Vec::new(); for (_, animals) in registry { for animal in animals { if !res.contains(animal) { res.push(animal.clone()); } } } 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 entry = registry.get_mut(section); match entry { Some(vec) => { if !vec.contains(&animal.to_string()) { vec.push(animal.to_string()); } }, None => { let vec = vec![animal.to_string()]; registry.insert(section.to_string(), vec); } }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let entry = registry.get(section); match entry { Some(vec) => { let mut tosort = vec.clone(); tosort.sort(); tosort }, None => { Vec::new() } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all = Vec::new(); for (_k,animals) in registry { for a in animals { all.push(a.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![]); 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).unwrap_or(&vec![]).to_vec(); 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 vec = registry.entry(section.to_string()).or_insert(vec![]); if !vec.contains(&animal.to_string()) { vec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(v) = registry.get(section) { let mut v = v.clone(); v.sort(); return v; } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut v = vec![]; for item in registry.values() { v.extend_from_slice(item); } 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 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> { // TODO: implement this function match registry.get(section) { Some(v) => { let mut sor = v.clone(); sor.sort(); sor }, None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v: Vec<String> = vec![]; for k in registry.values() { v.extend_from_slice(k); } 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 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> { // TODO: implement this function match registry.get(section) { Some(v) => { let mut sor = v.clone(); sor.sort(); sor }, None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v: Vec<String> = vec![]; for k in registry.values() { v.extend_from_slice(k); } 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 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> { // TODO: implement this function match registry.get(section) { Some(v) => { let mut sor = v.clone(); sor.sort(); sor }, None => Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v: Vec<String> = Vec::new(); for k in registry.values() { v.extend_from_slice(k); } 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) { registry .entry(section.to_string()) .and_modify(|sect| { if !sect.contains(&animal.to_string()) { sect.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(sect) = registry.get(section) { let mut section_sorted = sect.clone(); section_sorted.sort(); section_sorted } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = Vec::new(); registry .iter() .for_each(|(_, list)| animals.extend_from_slice(list)); 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(String::from(section), vec!(animal.to_string())); } else { registry.entry(section.to_string()).and_modify(|animals| { if animals.contains(&animal.to_string()) { return } 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(§ion.to_string()) { let mut section_animals = Vec::new(); section_animals.extend(animals.clone()); section_animals.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); return section_animals; } Vec::new()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals = Vec::new(); for animals in registry.values() { for animal in animals.iter() { all_animals.push(animal.to_string()); } } all_animals.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); all_animals}