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 if let Some(col) = registry.get_mut(section) { if let None = col.iter().find(|&s| s == animal) { col.push(animal.to_owned()); } }else{ registry.insert(section.to_owned(), vec![animal.to_owned()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(col) => { let mut res = col.to_owned(); res.sort(); res}, None => { vec![] }, }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res: Vec<String> = registry.values().flatten().cloned().collect(); 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) { // TODO: implement this function if let Some(col) = registry.get_mut(section) { if let None = col.iter().find(|&s| s == animal) { col.push(animal.to_owned()); } }else{ registry.insert(section.to_owned(), vec![animal.to_owned()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(col) => { let mut res = col.to_owned(); res.sort(); res}, None => { vec![] }, }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res = Vec::new(); for (_k, v) in registry.iter(){ let mut c= v.iter().map(|x| x.to_owned() ).collect::<Vec<String>>(); res.append(&mut c) } 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) { // TODO: implement this function if let Some(sec)=registry.get_mut(section) { if !sec.contains(&animal.to_string()) { sec.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> { // TODO: implement this function let mut all: Vec<String> = vec!(); if let Some(sec)=registry.get(section) { all.extend(sec.clone()) } all.sort(); all}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<String> = vec!(); for (_,v) in registry.iter() { all.extend(v.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 if let Some(sec)=registry.get_mut(section) { if !sec.contains(&animal.to_string()) { sec.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> { // TODO: implement this function let mut all: Vec<String> = vec!(); if let Some(sec)=registry.get(section) { all.extend(sec.clone()) } all.sort(); all}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<String> = vec!(); for (_,v) in registry.iter() { all.extend(v.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 registry.contains_key(&String::from(section)) && !registry.get(&String::from(section)).unwrap_or(&vec![]).to_vec().contains(&String::from(animal)) { registry.entry(String::from(section)).and_modify(|e| {e.push(String::from(animal));}); } else if !registry.contains_key(&String::from(section)) { registry.insert(String::from(section), vec![String::from(animal)]); } }pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(&String::from(section)) { Some(arr) => {let mut arr = arr.to_vec(); arr.sort(); arr.dedup(); arr} None => return vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); animals.dedup(); 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(section) { if !registry.get(section).unwrap().contains(&String::from(animal)) { registry.get_mut(section).unwrap().push(String::from(animal)); } } else { registry.insert(String::from(section), vec![String::from(animal)]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if registry.contains_key(section) { let mut res = registry.get(section).unwrap().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.values() { res.append(&mut animals.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) { if registry.contains_key(section) { if !registry.get(section).unwrap().contains(&String::from(animal)) { registry.get_mut(section).unwrap().push(String::from(animal)); } } else { registry.insert(String::from(section), vec![String::from(animal)]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if registry.contains_key(section) { let mut res = registry.get(section).unwrap().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.values() { res.append(&mut animals.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) { // TODO: implement this function match registry.get_mut(section) { Some(animals) => { if !animals.contains(&animal.to_string()){ animals.push(animal.to_string()); } }, None => { let mut animals: Vec<String> = Vec::new(); animals.push(animal.to_string()); registry.insert(section.to_string(), 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 res: Vec<String> = animals.clone(); res.sort(); return res; } None => { return Vec::new(); } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut res: Vec<String> = Vec::new(); for animals in registry.values(){ for animal in animals.iter(){ res.push(animal.to_string()); } } 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 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 if registry.contains_key(section) { let mut animals = registry.get(section).unwrap().clone(); animals.sort(); animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = Vec::new(); for (_, animals) in registry.iter() { result.extend(animals.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.get(section); if animals.is_some() { for value in animals.unwrap().iter() { if animal == value { return; } } } registry.entry(section.to_string()).or_insert_with(Vec::new).push(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_else(Vec::new)}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = registry .values() .flat_map(|v| v.iter().cloned()) .collect(); animals.sort(); animals}
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 registry.contains_key(section) { let mut animals = registry.get(section).unwrap().clone(); animals.sort(); animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = Vec::new(); for (_, animals) in registry.iter() { result.extend(animals.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(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 animal = animals.clone(); animal.sort(); return animal } else { return Vec::new(); }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals_all:Vec<String> = Vec::new(); for (_,animals) in registry.iter() { animals_all.extend(animals.clone()); } animals_all.sort(); return animals_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(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 animal = animals.clone(); animal.sort(); return animal } else { return Vec::new(); }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals_all:Vec<String> = Vec::new(); for (_,animals) in registry.iter() { animals_all.extend(animals.clone()); } animals_all.sort(); return animals_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(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 animal = animals.clone(); animal.sort(); return animal } else { return Vec::new(); }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals_all:Vec<String> = Vec::new(); for (_,animals) in registry.iter() { animals_all.extend(animals.clone()); } animals_all.sort(); return animals_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> { if let Some(animals) = registry.get(section) { let mut animal = animals.clone(); animal.sort(); return animal } else { return Vec::new(); }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals_all: Vec<String> = Vec::new(); for (_,animals) in registry.iter() { animals_all.extend(animals.clone()); } animals_all.sort(); return animals_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(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 animal = animals.clone(); animal.sort(); return animal } else { return Vec::new(); }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals_all:Vec<String> = Vec::new(); for (_,animals) in registry.iter() { animals_all.extend(animals.clone()); } animals_all.sort(); return animals_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(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(v) = registry.get(section) { let mut animals = v.to_vec(); animals.sort(); return animals; } else { return vec![]; }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut list: Vec<String> = Vec::new(); for (_, val) in registry.iter() { list.extend(val.clone()); } list.sort(); return 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 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> { 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 animals: Vec<_> = 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_default(); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry.get(section) .map(|v| { let mut v = v.clone(); v.sort(); v }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<_> = 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_default(); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry.get(section) .map(|v| { let mut v = v.clone(); v.sort(); v }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<_> = 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 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 if let Some(animals) = registry.get(section) { let mut copy = animals.clone(); copy.sort(); return copy; } return vec![];}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut ans = registry.values().flatten().cloned().collect::<Vec<_>>(); 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) { // 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 if let Some(animals) = registry.get(section) { let mut copy = animals.clone(); copy.sort(); return copy; } return vec![];}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut ans = registry.values().flatten().cloned().collect::<Vec<_>>(); 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) { // TODO: implement this function let entry = registry.entry(section.to_string()); match entry { std::collections::hash_map::Entry::Occupied(mut occupied_entry) => { let v = occupied_entry.get_mut(); if v.contains(&animal.to_string()) == false{ v.push(animal.to_string()); } }, std::collections::hash_map::Entry::Vacant(vacant_entry) => { vacant_entry.insert(vec![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 copy = animals.clone(); copy.sort(); return copy; } return vec![];}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = vec![]; for (_, value) in registry.iter() { all_animals.extend(value.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 should not duplicate animals if let Some(animals) = registry.get_mut(section) { 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> { // TODO: implement this function should short lexicographically if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function should short lexicographically 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) { // TODO: implement this function let animals = registry.entry(section.to_string()).or_insert_with(Vec::new); // Add the animal to this section's vector // Only add if animal doesn't already exist in this section 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 match registry.get(section) { Some(animals) => { let mut sorted = animals.clone(); sorted.sort(); sorted } None => Vec::new() }}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 section = registry.entry(section.to_string()).or_insert_with(Vec::new); if !section.contains(&animal.to_string()) { section.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.get(section).cloned().unwrap_or(Vec::new()); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.values().cloned().flatten().collect::<Vec<String>>(); animals.sort(); animals}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal = animal.to_string(); let section = section.to_string(); registry.entry(section).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> { registry .get(section) .map(|animals| { let mut res = animals.to_vec(); res.sort(); res }) .unwrap_or_else(Vec::new)}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res: Vec<String> = registry .keys() .flat_map(|section| get_animals_in_section(section, registry)) .collect(); 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) { 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> { match registry.get(section) { Some(animals) => { let mut animals = animals.to_vec(); animals.sort(); animals.to_owned() } None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut list = registry .iter() .map(|(_, v)| v.to_owned()) .fold(vec![], |mut pre, next| { pre.extend_from_slice(&next[..]); pre }); 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) { let animals = registry.entry(section.into()).or_insert(vec![]); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let animals = registry.get(section.into()); match animals { Some(list) => { let mut list = list[..].to_vec(); list.sort_by(|a, b| a.cmp(b)); list } None => Vec::<String>::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut list = registry .iter() .map(|(_, v)| v.to_owned()) .fold(vec![], |pre, next| [pre, next].concat()); list.sort_by(|a, b| a.cmp(&b)); list}
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| { match animals.contains(&animal.to_string()) { false => animals.push(animal.to_string()), true => (), } }).or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let res = registry.get(section); match res { Some(x) => {let mut sorted_vec = x.to_vec(); sorted_vec.sort(); return sorted_vec;}, None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res : Vec<String>= Vec::new(); for (_key, value) in registry.iter() { res.extend_from_slice(&value[..]); } 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 if !registry.contains_key(section) { registry.insert(section.to_string(), Vec::new()); } let sec_animals = registry.get_mut(section).unwrap(); if !sec_animals.contains(&animal.to_string()) { sec_animals.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::new(); } let mut sec_animals = registry.get(section).unwrap().clone(); sec_animals.sort(); sec_animals.to_vec()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals = Vec::new(); for (_, v) in registry.iter() { all_animals.extend(v.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 regis = registry.entry(section.to_string()).or_default(); if !regis.contains(&animal.to_string()) { regis.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut mut_regis = registry.clone(); let regis = mut_regis.get_mut(§ion.to_string()); match regis { Some(col) => { col.sort(); println!("{:?}",col.to_vec()); col.to_vec() }, None => { vec![] } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut tmp = Vec::new(); for (_,animals) in registry { tmp.extend(animals.clone()); } tmp.sort(); tmp}
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 r = registry.entry(section.to_owned()).or_default(); if !r.iter().any(|s| s == animal) { r.push(animal.to_owned()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(§ion.to_owned()) { Some(v) => { let mut r = v.clone(); r.sort(); r } None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<String> = Vec::new(); for (_,v) in registry.iter() { for animal in v { if !result.iter().any(|s| s == animal) { result.push(animal.to_owned()); } } } 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_default(); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry .get(section) .map_or_else(Vec::new, |animals| { let mut sorted = animals.clone(); sorted.sort(); sorted })}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut sorted_animals: Vec<String> = registry .values() .flat_map(|animals| animals.iter().cloned()) .collect(); 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) { 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: Vec<String> = match registry.get(section) { Some(animals) => animals.to_vec(), None => Vec::new(), }; animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut sorted_animals: Vec<String> = Vec::new(); registry .iter() .for_each( |(_section, animals)| { animals .iter() .for_each( |animal| { sorted_animals.push(animal.to_string()); } ) } ); 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) { 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> { match registry.get(section) { Some(animals) => { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals } None => vec![], }}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 animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(animals) => { let mut sorted_animals = animals.clone(); sorted_animals.sort(); return sorted_animals; } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut collec = Vec::new(); for (sec, _) in registry.iter() { let animals = get_animals_in_section(sec, registry); collec.extend(animals.clone()); } collec.sort(); return collec;}
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 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> = 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 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> { if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); // clone the list sorted_animals.sort(); // sort alphabetically sorted_animals } else { Vec::new() // section not found }}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 entry = registry.entry(section.to_string()).or_insert_with(Vec::new); // Mengecek apakah animal sudah ada di dalam Vec if !entry.contains(&animal.to_string()) { entry.push(animal.to_string()); // Menambahkan animal jika belum ada }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut new_vec = registry.get(section).unwrap_or(&Vec::new()).to_vec(); new_vec.sort(); new_vec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut v: Vec<String> = Vec::new(); for (_, val) in registry.iter() { v.extend(val.to_vec()); } 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 entry = registry.entry(section.to_string()).or_insert_with(Vec::new); // Mengecek apakah animal sudah ada di dalam Vec if !entry.contains(&animal.to_string()) { entry.push(animal.to_string()); // Menambahkan animal jika belum ada }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut new_vec = registry.get(section).unwrap_or(&Vec::new()).to_vec(); new_vec.sort(); new_vec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut v: Vec<String> = Vec::new(); for (_, val) in registry.iter() { v.extend(val.to_vec()); } 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::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(); return sorted_animals; } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut collec = Vec::new(); for (sec, _) in registry.iter() { let animals = get_animals_in_section(sec, registry); collec.extend(animals.clone()); } collec.sort(); return collec;}
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) { let anis_in_section = registry.get_mut(section).unwrap(); anis_in_section.push(animal.to_string()); anis_in_section.dedup(); } 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(animals) => { let mut a: Vec<String> = animals.to_vec(); a.sort(); a } None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = Vec::new(); for (_, v) in registry.iter() { all_animals.extend(v.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 section_animals = registry.entry(section.to_string()).or_insert(vec![]); // If the animal is not already in the list, add it 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> { // TODO: implement this function if let Some(animals) = registry.get(section){ let mut section_animals = animals.clone(); section_animals.sort(); section_animals } else{ vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals = Vec::new(); for (section, animals) in registry.iter(){ all_animals.extend(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) { let section_animals = registry .entry(section.to_string()) .or_insert(Vec::new()); let animal = animal.to_string(); if !section_animals.contains(&animal) { section_animals.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(§ion.to_string()) { Some(vv) => { let mut result = vv.clone(); result.sort_unstable(); result }, _ => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = registry.values() .map(|v| v.clone()) .flatten() .collect::<Vec<String>>(); 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) { let section_element = registry.entry(section.to_string()).or_default(); if !section_element.contains(&animal.to_string()) { section_element.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut final_vect = match registry.get(§ion.to_string()) { Some(value) => value.clone(), None => Vec::new() }; final_vect.sort(); final_vect}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut final_vect: Vec<String> = Vec::new(); for (_, section_element) in registry { final_vect.append(&mut section_element.clone()); } final_vect.sort(); return final_vect;}
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_element = registry.entry(section.to_string()).or_default(); if !section_element.contains(&animal.to_string()) { section_element.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut final_vect = match registry.get(§ion.to_string()) { Some(value) => value.clone(), None => Vec::new() }; final_vect.sort(); final_vect}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut final_vect: Vec<String> = Vec::new(); for (_, section_element) in registry { final_vect.extend(section_element.clone()); } final_vect.sort(); return final_vect;}
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_element = registry.entry(section.to_string()).or_default(); if !section_element.contains(&animal.to_string()) { section_element.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut final_vect = match registry.get(§ion.to_string()) { Some(value) => value.clone(), None => Vec::new() }; final_vect.sort(); final_vect}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut final_vect: Vec<String> = Vec::new(); for (_, section_element) in registry { final_vect.append(&mut section_element.clone()); } final_vect.sort(); return final_vect;}
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 animals = animals.clone(); animals.sort(); animals } None => { Vec::new() } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut output: Vec<String> = vec![]; for (_, animals) in registry { let animals = animals.clone(); output.extend(animals); } output.sort(); output}
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); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); registry.insert(section.to_string(), animals); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function if let Some(result) = registry.get(§ion.to_string()) { let mut sorted_result = result.clone(); sorted_result.sort(); return sorted_result; } else { return Vec::new(); }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vec: Vec<String> = Vec::new(); for val in registry.values() { vec.append(&mut val.to_vec()); } vec.sort(); vec}