"Santa, we need to talk!" Snowball stammered, holding a stack of printouts that smelled vaguely of desperation.
Santa looked up, his patience thinner than Rudolph’s battery life. "What now?"
Bernard, the senior elf, didn’t mince words. "It’s the Naughty-Nice List system. Legacy code. Written in JavaScript."
Santa froze. "Not even TypeScript?"
Bernard shook his head. "Plain JavaScript. var
everywhere. No types. No safety. It’s a dumpster fire. Someone even polyfilled Promise
with copy-paste."
Santa slammed his mug down. "So Naughty kids get PS5s, and Nice kids get socks because JavaScript?"
Snowball nodded, sweating. "It’s worse, Santa. There’s no validation. The Nice List has a SELECT *
injection, and someone added console.log("Merry Christmas, LOL");
in production!"
Santa pinched the bridge of his nose. "Rewriting it in Rust is our only hope. No globals, no runtime panics—safety guaranteed. Bernard, make it happen."
"But Santa," Bernard hesitated, "starting from scratch this late… it’s risky."
Santa leaned forward, eyes blazing. "Riskier than trusting JavaScript on Christmas Eve? I don’t care how late it is. Write it in Rust."
Implement a SantaList
struct that uses a HashMap
to store children’s names as keys and their behaviors (true
for nice, false
for naughty) as values.
The SantaList
struct should have a single field:
records
- a HashMap<String, bool>
to store children’s names and behaviors.The struct should have the following methods and associated functions:
new
- Create a new SantaList
instance.add
- Add a child's name and behavior to the list.remove
- Remove a child from the list.get
- Retrieve a child's behavior.count
- Count the number of nice and naughty children as a tuple (nice, naughty)
list_by_behavior
- Retrieve a list of children based on their behavior as Vec<String>
If you’re unsure where to start, take a look at these tips:
Using HashMap
: Import it with use std::collections::HashMap;
. Use HashMap::new()
to create an empty map.
Adding/Updating: Use insert(key, value)
to add or update an entry.
Querying: Use get(key)
to retrieve values. It returns an Option<bool>
.
Removing: Use remove(key)
to delete an entry and get its value if it exists.
Counting: Use values()
with .filter()
to count nice/naughty children:
use std::collections::HashMap;pub struct SantaList { pub records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> SantaList { SantaList { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { Some(*(self.records.get(name)?)) } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let filtered_count = self.records.values().filter(|&x| *x).count(); (filtered_count, self.records.len() - filtered_count) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.records .iter() .filter_map(|(k, v)| if *v == nice { Some(k.clone()) } else { None }) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field pub records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { let v = self.records.get(name)?; Some(*v) } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { ( self.records.iter().filter(|r| *r.1 == true).count(), self.records.iter().filter(|r| *r.1 == false).count(), ) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|r| *r.1 == behavior) .map(|r| r.0.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field children: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { children: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.children.insert(name.to_string(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.children.remove_entry(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { let value = self.children.get(name)?; Some(*value) } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice_count = self.children.iter().filter(|t| *t.1).count(); (nice_count, self.children.len() - nice_count) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.children .iter() .filter(|t| *t.1 == nice) .map(|(key, _)| key.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { children: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { children: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, is_nice: bool) { self.children.insert(name.to_string(), is_nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.children.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.children.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let mut nice = 0; let mut naughty = 0; for &is_nice in self.children.values() { if is_nice { nice += 1; } else { naughty += 1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { self.children .iter() .filter(|&(_, &value)| value == is_nice) .map(|(key, _)| key.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field pub records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, child_name: &str, is_nice: bool) { self.records.insert(child_name.to_string(), is_nice); } // 4. Implement the remove method pub fn remove(&mut self, child_name: &str) { self.records.remove_entry(child_name); } // 5. Implement the get method pub fn get(&self, child_name: &str) -> Option<bool> { self.records.get(child_name).cloned() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut nice_count = 0; let mut naughty_count = 0; for (_, is_nice) in self.records.iter() { if *is_nice { nice_count += 1; } else { naughty_count += 1; } } (nice_count, naughty_count) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|m| *m.1 == behavior) .map(|m| m.0.clone()) .collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::<String, bool>::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { let _ = self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { match self.records.get(name) { Some(name) => Some(*name), None => None } } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut nice = 0; let mut naughty = 0; for value in self.records.values() { if *value { nice += 1; } else { naughty += 1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_, &value)| value == behavior) .map(|(key, _)| key.to_string()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { ( self.list_by_behavior(true).len(), self.list_by_behavior(false).len(), ) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|&(_name, &val)| val == behavior) .map(|(name, _val)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}#[cfg(test)]mod test { use crate::SantaList; #[test] fn test_add_remove() { let mut list = SantaList::new(); list.add("Alice", true); list.add("Bob", false); assert_eq!(list.get("Alice"), Some(true)); assert_eq!(list.get("Bob"), Some(false)); assert_eq!(list.get("Charlie"), None); list.remove("Bob"); assert_eq!(list.get("Bob"), None); }}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name:&str) { let result = self.records.remove(name); if result.is_none() { eprintln!("RemoveError: No entry {name} to remove"); } } // 5. Implement the get method pub fn get(&self, name:&str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count (&self) -> (usize, usize) { let mut nice: usize = 0; let mut naughty: usize = 0; for behavior in self.records.values() { if *behavior { nice += 1; } else { naughty += 1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut result = Vec::new(); for (key, _) in self.records.iter().filter(|(_, v)| **v == behavior ) { result.push(String::from(key.clone())) } result }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(String::from(name), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice_count = self.records.iter() .filter(| (_, v)| **v) .count(); let naughty_count = self.records.iter() .filter(|(_, v)| !**v) .count(); (nice_count, naughty_count) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|(_, v)| **v == behavior) .map(|(k, _)| k.clone()) .collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { pub records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new(), } } pub fn add(&mut self, name: impl Into<String>, value: bool) { self.records.insert(name.into(), value); } pub fn remove(&mut self, name: impl Into<String>) { self.records.remove(&name.into()); } pub fn get(&self, name: impl Into<String>) -> Option<bool> { self.records.get(&name.into()).copied() } pub fn count(&self) -> (usize, usize) { self.records.values().fold((0, 0), |mut count, v| { if *v { count.0 += 1 } else { count.1 += 1 } count }) } pub fn list_by_behavior(&self, value: bool) -> Vec<String> { self.records .iter() .filter_map(|(k, v)| if *v == value { Some(k.clone()) } else { None }) .to_owned() .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList{ // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, child_name: &str, behavior: bool) -> () { self.records.insert(child_name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, child_name: &str) -> () { if self.records.contains_key(child_name) { self.records.remove(child_name); } } // 5. Implement the get method pub fn get(&self, child_name: &str) -> Option<bool>{ self.records.get(child_name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { ( self.records.values().filter(|item| **item).count(), self.records.values().filter(|item| !**item).count() ) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_key, val)| **val == behavior) .map(|(key, _val)| key.to_string()) .collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { let new_recs = HashMap::new(); Self { records: new_recs } } // 3. Implement the add method pub fn add(&mut self, child: &str, niceness: bool) { self.records.insert(child.to_string(), niceness); } // 4. Implement the remove method pub fn remove(&mut self, child: &str) { self.records.remove(child); } // 5. Implement the get method pub fn get(&self, child: &str) -> Option<bool> { if let Some(&value) = self.records.get(child) { Some(value) } else { None } } // 6. Implement the count method pub fn count(&self) -> (i32, i32) { let mut nice = 0; let mut naughty = 0; for val in self.records.values() { if *val == true { nice = nice + 1 } else { naughty = naughty + 1 } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { let mut nice_map = self.records.clone(); nice_map.retain(|_, v| *v == true); let mut naughty_map = self.records.clone(); naughty_map.retain(|_, v| *v == false); let mut list: Vec<String> = vec![]; if behaviour { for key in nice_map.keys() { list.push(key.to_string()) } } else { for key in naughty_map.keys() { list.push(key.to_string()) } } list }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { ( self.records.iter().filter(|kid| kid.1 == &true).count(), self.records.iter().filter(|kid| kid.1 == &false).count(), ) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter_map(|(key, &value)| if value == behavior { Some(key.clone()) } else { None }) .collect() }}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, is_nice: bool) { self.records.insert(name.to_string(), is_nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let good = self.records.values().filter(|v| **v == true).count(); let naughty = self.records.values().count() - good; (good, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { self.records .iter() .filter_map(|(k, v)| { if *v == is_nice { Some(k.clone()) } else { None } }) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;// use std::error::Error;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { if let Some(b) = self.records.get(name) { Some(*b) } else { None } } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let good = self.records.values().filter(|v| **v == true).count(); let naughty = self.records.values().count() - good; (good, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool)-> Vec<String> { self.records .iter() .filter_map(|(k, v)| { if *v == behavior { Some(k.clone()) } else { None } } ).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { (self.records.iter().filter(|(_k, v)| **v == true).count(), self.records.iter().filter(|(_k, v)| **v == false).count()) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter_map(|(k, v)| { if *v == behavior { Some(k.clone()) } else { None } } ).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;#[derive(Default)]pub struct SantaList { // 1. Define the records field pub children: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self::default() } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.children.insert(name.into(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.children.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.children.get(name).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { (self._count_behavior(true), self._count_behavior(false)) } pub fn _count_behavior(&self, behavior: bool) -> usize { self.children .iter() .filter(|(_, value)| **value == behavior) .count() } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.children .iter() .filter(|(_, value)| **value == behavior) .map(|(key, _)| key.clone() ) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, is_nice: bool) { self.records.insert(name.to_string(), is_nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method // Count the number of nice and naughty children as a tuple (nice, naughty) pub fn count(&self) -> (i64, i64) { let naughty_count = self.list_by_behavior(false).len(); let nice_count = self.list_by_behavior(true).len(); (nice_count as i64 , naughty_count as i64) } // 7. Implement the list_by_behavior method // Retrieve a list of children based on their behavior as Vec<String> pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records.iter() .filter(|(_, &is_nice)| is_nice == behaviour) .map(|(name, _)| name.to_string()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self{ records: HashMap::new() } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } pub fn get(&self, child: &str) -> Option<bool> { match self.records.get(child) { Some(&b) => Some(b), None => None } } pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&a| *a).count(); let naughty = self.records.values().filter(|&a| !*a).count(); (nice, naughty) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|a| a.1 == &behavior).map(|a| a.0.clone()).collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method // 3. Implement the add method // 4. Implement the remove method // 5. Implement the get method // 6. Implement the count method // 7. Implement the list_by_behavior method pub fn new() -> Self { Self{ records: HashMap::new() } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } pub fn get(&self, child: &str) -> Option<bool> { match self.records.get(child) { Some(&b) => Some(b), None => None } } pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&a| *a).count(); let naughty = self.records.values().filter(|&a| !*a).count(); (nice, naughty) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|a| a.1 == &behavior).map(|a| a.0.clone()).collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList(HashMap<String, bool>);impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self(HashMap::new()) } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.0.insert(String::from(name), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.0.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.0.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let nice = self.0.values().filter(|&&behavior| behavior).count() as u32; let naughty = self.0.values().filter(|&&behavior| !behavior).count() as u32; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.0 .iter() .filter(|&(_, &beh)| beh == behavior) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self{ records: HashMap::new() } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } pub fn get(&self, child: &str) -> Option<bool> { match self.records.get(child) { Some(&b) => Some(b), None => None } } pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&a| *a).count(); let naughty = self.records.values().filter(|&a| !*a).count(); (nice, naughty) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|a| a.1 == &behavior).map(|a| a.0.clone()).collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, child: &str, behavior: bool) { self.records.insert(child.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, child: &str) { self.records.remove(child); } // 5. Implement the get method pub fn get(&self, child: &str) -> Option<bool> { match self.records.get(child) { Some(&b) => Some(b), None => None } } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut g: u32 = 0; let mut len: u32 = 0; for (_, &b) in self.records.iter() { if b { g += 1; } len += 1 } (g, len - g) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut buf: Vec<String> = Vec::new(); for (s, &b) in self.records.iter() { if b == behavior { buf.push(s.to_string()); } } buf }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,} impl SantaList { // 2. Implement the new method pub fn new() -> Self{ Self{ records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behaviour: bool){ self.records.insert(name.into(), behaviour); } // 4. Implement the remove method pub fn remove(&mut self, name: &str){ if self.records.contains_key(name){ self.records.remove(name).unwrap(); } } // 5. Implement the get method pub fn get(&self, name: &str)-> Option<bool>{ self.records.get(name.into()).cloned() } // 6. Implement the count method pub fn count(&self)-> (u32, u32){ let (nice, naughty) = self.records.iter() .fold((0,0), |(nice, naughty), (_, &val)|{ if val{ (nice + 1, naughty) }else{ (nice, naughty + 1) } } ); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records .iter() .filter(|x| *x.1 == behaviour) .map(|y| y.0.clone()) .collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field list: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self{ list: HashMap::new()} } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.list.insert(name.to_string(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.list.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool>{ let val = self.list.get(name); if val.is_none() { return None } Some(val.unwrap().clone()) } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice_count = self.list.values() .filter(|&nice| *nice == true) .count(); let naughty_count = self.list.len() - nice_count; (nice_count, naughty_count) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.list.iter() .filter(|(name, &nice)| nice == behavior) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new() } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).cloned() } pub fn count(&self) -> (usize, usize) { let mut nice_count: usize = 0; let mut naughty_count: usize = 0; for (name, behavior) in &self.records { if *behavior { nice_count += 1; } else { naughty_count += 1; } } (nice_count, naughty_count) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|&(_, b)| b == &behavior) .map(|(name, _)| name.clone()) .collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, niceness: bool) { self.records.insert(name.to_string(), niceness); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&name.to_string()).cloned() } // 6. Implement the count method pub fn count(&self) -> (i32, i32) { let mut nice = 0; let mut naughty = 0; for &niceness in self.records.values() { if niceness { nice += 1; } else { naughty += 1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, niceness: bool) -> Vec<String> { self.records .iter() .filter(|&(_, val)| *val == niceness) .map(|(n,_)| n.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } pub fn add(&mut self, child_name: &str, behaves: bool) { self.records.insert(child_name.to_string(), behaves); } pub fn get(&self, child_name: &str) -> Option<bool> { self.records.get(child_name).copied() } pub fn count(&self) -> (u32, u32) { let mut nice: u32 = 0; let mut naughty: u32 = 0; for is_nice in self.records.values() { if *is_nice { nice += 1; } else { naughty += 1; } } (nice, naughty) } pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { self.records .iter() .filter_map(|(name, nice)| { if *nice == is_nice { Some(name.clone()) } else { None } }) .collect() } pub fn remove(&mut self, arg: &str) { self.records.remove(arg); }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } pub fn add(&mut self, child_name: &str, behaves: bool) { self.records.insert(child_name.to_string(), behaves); } pub fn get(&self, child_name: &str) -> Option<bool> { self.records.get(child_name).copied() } pub fn count(&self) -> (u32, u32) { let mut nice: u32 = 0; let mut naughty: u32 = 0; for is_nice in self.records.values() { if *is_nice { nice += 1; } else { naughty += 1; } } (nice, naughty) } pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { self.records .iter() .filter_map(|(name, nice)| { if *nice == is_nice { Some(name.clone()) } else { None } }) .collect() } pub fn remove(&mut self, arg: &str) { self.records.remove(arg); }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,} impl SantaList { // 2. Implement the new method pub fn new () -> Self { SantaList{records: HashMap::new()} } // 3. Implement the add method pub fn add(&mut self, key: &str, value: bool) { self.records.insert(key.to_string(), value); } // 4. Implement the remove method pub fn remove (&mut self, key: &str) { self.records.remove(key); } // 5. Implement the get method pub fn get(&self, key: &str) -> Option<bool> { self.records.get(key).copied() } // 6. Implement the count method pub fn count (&self) -> (u64, u64) { let nice_count = self.records.values().filter(|&&v| v).count() as u64; let naughty_count = self.records.len() as u64 - nice_count; (nice_count, naughty_count) } // 7. Implement the list_by_behavior method pub fn list_by_behavior (&self, behavior: bool) -> Vec<String> { self.records.iter().filter_map(|(key, &value)| if value == behavior {Some(key.clone())} else{None}).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&name.to_string()).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&nice| *nice).count(); let naughty = self.records.values().filter(|&nice| !*nice).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, desired_nice: bool) -> Vec<String> { self.records.iter().filter_map(|(&ref k, &is_nice)| { if is_nice == desired_nice { Some(k.clone()) } else { None } }).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&name.to_string()).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&nice| *nice).count(); let naughty = self.records.values().filter(|&nice| !*nice).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, desired_nice: bool) -> Vec<String> { self.records.iter().filter_map(|(&ref k, &is_nice)| { if is_nice == desired_nice { Some(k.clone()) } else { None } }).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.iter() .filter(|&(_, value)| *value) .collect::<Vec<_>>() .len(); let naughty = self.records.len() - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|&(_, value)| *value == behavior) .map(|(key, _)| key.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { children: HashMap<String, bool>, // 1. Define the records field}impl SantaList { pub fn new() -> SantaList { SantaList{children: HashMap::new()} } pub fn add(&mut self, name: &str, value: bool) { self.children.insert(name.to_string(), value); } pub fn remove(&mut self, name: &str) { self.children.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.children.get(name).copied() } pub fn count(&self) -> (usize, usize) { let count = self.children.len(); let nice = self.children.values().filter(|v| **v).into_iter().count(); (nice, count - nice) } pub fn list_by_behavior(&self, value: bool) -> Vec<String> { self.children .iter() .filter(|(_, &v)| v == value) .map(|(name, _)| name.clone()) .collect() } // 2. Implement the new method // 3. Implement the add method // 4. Implement the remove method // 5. Implement the get method // 6. Implement the count method // 7. Implement the list_by_behavior method}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behaviour: bool) { self.records.insert(name.to_string(), behaviour); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&name.to_string()).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { self.records .iter() .fold((0, 0), |(mut good, mut bad), entry| { if *entry.1 { (good += 1, bad); } else { (good, bad += 1); } (good, bad) }) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records .iter() .filter(|entry| entry.1 == &behaviour) .map(|entry| entry.0.to_string()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}// dim_date.weekday, dim_product.category;
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { let _ = self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { self.records.iter().fold((0, 0), |acc, x| match x.1 { true => (acc.0 + 1, acc.1), // nice false => (acc.0, acc.1 + 1), // naughty }) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter(). filter(|x| *x.1 == behavior). map(|y| y.0.clone()). collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) ->Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&b| *b).count(); let naughty = self.records.values().filter(|&b|!*b).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.records .iter() .filter(|(_, &behaviour)| behaviour == nice) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut nice = 0_u32; let mut naughty = 0_u32; for (_, is_nice) in &self.records { if *is_nice { nice += 1; } else { naughty += 1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter_map(|(n, b)| { if *b == behavior { Some(n.clone()) } else { None } }) .collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) -> () { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) -> () { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let mut nice = 0; let mut naughty = 0; for &v in self.records.values() { if v { nice += 1; } else { naughty += 1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|&(_, v)| *v == behavior) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) -> () { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) -> () { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records .values() .filter(|child| **child == true) .count(); let naughty = self.records.values().count() - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|&(_, v)| *v == behavior) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let mut nice = 0; let mut naughty = 0; self.records.iter().for_each(|(_, &behavior)| { if behavior { nice += 1; } else { naughty += 1; } }); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.records.iter().filter_map(|(name, &behavior)| { if behavior == nice { Some(name.to_string()) } else { None } }).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, child: &str, behavior: bool) { self.records.insert(child.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, child: &str) { self.records.remove(child); } // 5. Implement the get method pub fn get(&self, child: &str) -> Option<bool> { self.records.get(child).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut nice = 0; let mut naughty = 0; for &v in self.records.values() { if v { nice += 1; } else { naughty += 1; } }; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|&(_, v)| *v == behavior) .map(|(k, _)| k.clone()).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, key: &str, value: bool) { self.records.insert( String::from(key), value ); } // 4. Implement the remove method pub fn remove(&mut self, key: &str) { self.records.remove(&String::from(key)); } // 5. Implement the get method pub fn get(&self, key: &str) -> Option<bool> { self.records.get(&String::from(key)).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut nice: u32 = 0; let mut naughty: u32 = 0; for val in self.records.values() { if *val { nice += 1; } else { naughty +=1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|(_, &val)| val == behavior) .map(|(key, _)| key.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;use std::borrow::{Cow, Borrow};pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add<'a>(&mut self, name: impl Into<Cow<'a, str>>, nice: bool) { let name = name.into(); if let Some(record) = self.records.get_mut::<str>(name.borrow()) { *record = nice; } else { // only clone if needed let _ = self.records.insert(name.into_owned(), nice); } } // 4. Implement the remove method pub fn remove(&mut self, name: impl Borrow<str>) { let _ = self.records.remove(name.borrow()); } // 5. Implement the get method pub fn get(&self, name: impl Borrow<str>) -> Option<bool> { self.records.get(name.borrow()).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice_ct = self.records.values().filter(|&&nice| nice).count(); let naughty_ct = self.records.len() - nice_ct; (nice_ct, naughty_ct) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter_map(|(name, &nice)| (behavior == nice).then(|| name.to_owned())) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, is_nice: bool) { self.records.insert(String::from(name), is_nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&&v| v).count(); let naughty = self.records.values().filter(|&&v| !v).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { self.records .iter() .filter(|(_, &v)| { v == is_nice}) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.iter().filter(|&(_, &v)| v ).count(); let naughty = self.records.iter().filter(|&(_, &v)| !v ).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|&(_, &v)| v == behavior ) .map(|(k, _)| k.clone() ) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { let _ = self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { let _ = self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice_count = self.records.iter().filter(|&(_, &v)| v == true).count(); ( nice_count, self.records.iter().count() - nice_count, ) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut list: Vec<String> = vec![]; let sublist: Vec<(&String, &bool)> = self.records.iter().filter(|&(_, &v)| v == behavior).collect(); for rec in sublist { list.push(rec.0.clone()); } list }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { let records = HashMap::new(); SantaList{records} } // 3. Implement the add method pub fn add(self: &mut Self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(self: &mut Self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(self: &Self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(self: &Self) -> (usize, usize) { let nice = self.records.iter().filter(|r| *r.1 == true).count(); let naughty = self.records.iter().filter(|r| *r.1 == false).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(self: &Self, behavior: bool) -> Vec<String>{ self.records.iter().filter(|(_,&v)| v == behavior).map(|(k,_)| k.clone()).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String,bool>}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new() } } pub fn add(&mut self,name: &str, behavior:bool) { self.records.insert(name.into(),behavior); } pub fn remove(&mut self, name: &str) { let _ = self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (u32,u32) { self.records.values().fold((0,0), |(ni,na), &b| if b {(ni +1, na)} else {(ni,na+1)} ) } pub fn list_by_behavior(&self, behavior:bool) -> Vec<String> { self.records.iter().filter(|(_,&v)| v == behavior).map(|(k,_)| k.clone()).collect() } }pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let nice_count = self.records.values().filter(|&&behavior| behavior).count(); let naughty_count = self.records.len() - nice_count; (nice_count, naughty_count) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_, value)| **value == behavior) .map(|(key, _)| key.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}