"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 { // 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 );}
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() -> SantaList { SantaList { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.into(), nice); } // 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> { if let Some(&result) = self.records.get(name) { Some(result) } else { None } } // 6. Implement the count method pub fn count(&self) -> (i32, i32) { let mut list_count = (0, 0); for child in self.records.iter() { if *child.1 { list_count.0 += 1; } else { list_count.1 += 1; } } list_count } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut list: Vec<String> = vec![]; for child in self.records.iter() { if *child.1 == behavior { list.push(child.0.into()); } } 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 { 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(&String::from(name)); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&String::from(name)).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice: usize = self.records.values().filter(|&x| *x ).count(); let naughty: usize = self.records.values().count() - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { self.records.iter().filter(|&x| *x.1 == is_nice).map(|x| x.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;#[derive(Default)]pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Default::default() } 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) { self.records .iter() .fold((0, 0), |(nice, naughty), (_, &behavior)| { if behavior { (nice + 1, naughty) } else { (nice, naughty + 1) } }) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_, &b)| behavior == b) .map(|(name, _)| name.to_string()) .collect() }}
pub struct SantaList { // 1. Define the records field records: std::collections::HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: std::collections::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> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let (nice, naughty): (Vec<_>, Vec<_>) = self.records.iter().partition(|(_k,v)| **v); (nice.len(), naughty.len()) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|(_k,v)| **v == behavior).map(|(k,_v)| k.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 { let records = HashMap::new(); SantaList { records } } // 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 mut nice: usize = 0; let mut naughty: usize = 0; for (_k, v) in self.records.iter() { if *v { 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> { let mut result = Vec::new(); for (k, v) in self.records.iter() { if *v == is_nice { result.push(k.to_string()); } } 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 { 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>{ self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut nice = 0; let mut naughty = 0; for (_k,v) in self.records.iter() { 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> { let mut children = Vec::new(); for (k,v) in self.records.iter() { if *v == behavior { children.push(k.to_string()) } } children }}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) { let good = self.records.values().filter(|b| **b).count(); (good, self.records.len() - good) } // 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;// 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.// Fields//// The SantaList struct should have a single field://// records - a HashMap<String, bool> to store children’s names and behaviors.//// Methods//// 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>pub struct SantaList { records: HashMap<String, bool>, // 1. Define the records field}impl SantaList { pub fn new() -> SantaList { SantaList { records: HashMap::new(), } } pub fn add(&mut self, name: &str, behaviour: bool) -> Option<bool> { self.records.insert(name.to_string(), behaviour) } 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.iter().filter(|&(_k, v)| *v).count(); let bad_count = self.records.len() - nice_count; (nice_count, bad_count) } pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records .iter() .filter(|&(_k, v)| *v == behaviour) .map(|(k, _v)| 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() -> SantaList { let records = HashMap::<String, bool>::new(); SantaList{ records, } } // 3. Implement the add method pub fn add(&mut self, name: &str, is_nice: bool) { self.records.insert(name.to_owned(), 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 n = self.records.keys().len(); let naughty = self.records.iter().filter(|(_,v)|!*v).count(); (n-naughty, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, b: bool) -> Vec<String> { self.records.iter().filter(|&(_,v)|b == *v).map(|(k,_)|k.to_owned()).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 { pub fn new() -> SantaList { 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> { self.records.get(name).cloned() } // 6. Implement the count method pub fn count(& self) -> (usize, usize) { let nice = self.list_by_behavior(true).len(); let naughty = self.list_by_behavior(false).len(); (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 { records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> SantaList { 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 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> { let mut retval = Vec::new(); self.records.iter().for_each(|(key, value)| { if *value == behavior { retval.push(key.clone()); } }); retval }}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, record: &str, behavior: bool) { self.records.insert(record.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, record: &str) { self.records.remove(record); } // 5. Implement the get method pub fn get(&self, record: &str) -> Option<bool> { self.records.get(record).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.list_by_behavior(true).len(); let naughty = self.list_by_behavior(false).len(); (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;#[derive(Debug)]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, record: &str, behavior: bool) { self.records.insert(record.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, record: &str) { self.records.remove(record); } // 5. Implement the get method pub fn get(&self, record: &str) -> Option<bool> { self.records.get(record).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.list_by_behavior(true).len(); let naughty = self.list_by_behavior(false).len(); (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;#[derive(Debug)]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, record: &str, behavior: bool) { self.records.insert(record.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, record: &str) { self.records.remove(record); } // 5. Implement the get method pub fn get(&self, record: &str) -> Option<bool> { self.records.get(record).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.list_by_behavior(true).len(); let naughty = self.list_by_behavior(false).len(); (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 { 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) { self.records.values().fold((0, 0), |count, behavior| { if *behavior { (count.0 + 1, count.1) } else { (count.0, count.1 + 1) } }) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_, &v)| v == behavior) .map(|(k, _)| k.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::default()} } // 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 nices = self.records.iter().filter(|(_, v)| **v).count(); let not_nices = self.records.iter().filter(|(_, v)| !**v).count(); (nices, not_nices) } // 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 { let records = HashMap::new(); Self { records } } // 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 nice = self.records.values().filter(|&x| *x).count(); let naughty = self.records.values().filter(|&x| !*x).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() -> SantaList { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, value: bool) { self.records.insert(name.into(), value); } // 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) { let res = self.records.values().filter(|&&x| x).count(); (res, self.records.len() - res) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|&(_, x)| *x == behavior) .map(|(y, _)| y.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, 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.to_string()); } // 5. Implement the get method pub fn get(&self, key:&str) -> Option<bool> { self.records.get(key).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize,usize) { let mut counter = (0usize,0usize); self.records.iter().for_each(|(_,b)| { if *b { counter.0 += 1; }else { counter.1 += 1; } }); counter } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, good: bool) -> Vec<String> { let mut counter:Vec<String> = vec![]; self.records.iter().for_each(|(a,&b)| { if b == good { counter.push(a.clone()); } }); counter }}
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() -> 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> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let good: usize = self.records.values().filter(|v| **v == true).count(); let bad: usize = self.records.values().filter(|v| **v == false).count(); // return value (good, bad) } // 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() -> 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> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let good: usize = self.records.values().filter(|v| **v == true).count(); let bad: usize = self.records.values().filter(|v| **v == false).count(); // return value (good, bad) } // 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 { 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.into(), 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 mut number_of_nice_children = 0; let mut number_of_naughty_children = 0; for (_, &nice) in &self.records { if nice { number_of_nice_children += 1; } else { number_of_naughty_children += 1; } } (number_of_nice_children, number_of_naughty_children) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, was_nice: bool) -> Vec<String> { self.records .iter() .filter(|(_, &behaviour)| behaviour == was_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 );}