rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

hashmaps2.rs (2772B)


      1 // We're collecting different fruits to bake a delicious fruit cake. For this,
      2 // we have a basket, which we'll represent in the form of a hash map. The key
      3 // represents the name of each fruit we collect and the value represents how
      4 // many of that particular fruit we have collected. Three types of fruits -
      5 // Apple (4), Mango (2) and Lychee (5) are already in the basket hash map. You
      6 // must add fruit to the basket so that there is at least one of each kind and
      7 // more than 11 in total - we have a lot of mouths to feed. You are not allowed
      8 // to insert any more of the fruits that are already in the basket (Apple,
      9 // Mango, and Lychee).
     10 
     11 use std::collections::HashMap;
     12 
     13 #[derive(Hash, PartialEq, Eq, Debug)]
     14 enum Fruit {
     15     Apple,
     16     Banana,
     17     Mango,
     18     Lychee,
     19     Pineapple,
     20 }
     21 
     22 fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
     23     let fruit_kinds = [
     24         Fruit::Apple,
     25         Fruit::Banana,
     26         Fruit::Mango,
     27         Fruit::Lychee,
     28         Fruit::Pineapple,
     29     ];
     30 
     31     for fruit in fruit_kinds {
     32         // If fruit doesn't exist, insert it with some value.
     33         basket.entry(fruit).or_insert(5);
     34     }
     35 }
     36 
     37 fn main() {
     38     // You can optionally experiment here.
     39 }
     40 
     41 #[cfg(test)]
     42 mod tests {
     43     use super::*;
     44 
     45     // Don't modify this function!
     46     fn get_fruit_basket() -> HashMap<Fruit, u32> {
     47         let content = [(Fruit::Apple, 4), (Fruit::Mango, 2), (Fruit::Lychee, 5)];
     48         HashMap::from_iter(content)
     49     }
     50 
     51     #[test]
     52     fn test_given_fruits_are_not_modified() {
     53         let mut basket = get_fruit_basket();
     54         fruit_basket(&mut basket);
     55         assert_eq!(*basket.get(&Fruit::Apple).unwrap(), 4);
     56         assert_eq!(*basket.get(&Fruit::Mango).unwrap(), 2);
     57         assert_eq!(*basket.get(&Fruit::Lychee).unwrap(), 5);
     58     }
     59 
     60     #[test]
     61     fn at_least_five_types_of_fruits() {
     62         let mut basket = get_fruit_basket();
     63         fruit_basket(&mut basket);
     64         let count_fruit_kinds = basket.len();
     65         assert!(count_fruit_kinds >= 5);
     66     }
     67 
     68     #[test]
     69     fn greater_than_eleven_fruits() {
     70         let mut basket = get_fruit_basket();
     71         fruit_basket(&mut basket);
     72         let count = basket.values().sum::<u32>();
     73         assert!(count > 11);
     74     }
     75 
     76     #[test]
     77     fn all_fruit_types_in_basket() {
     78         let fruit_kinds = [
     79             Fruit::Apple,
     80             Fruit::Banana,
     81             Fruit::Mango,
     82             Fruit::Lychee,
     83             Fruit::Pineapple,
     84         ];
     85 
     86         let mut basket = get_fruit_basket();
     87         fruit_basket(&mut basket);
     88 
     89         for fruit_kind in fruit_kinds {
     90             let Some(amount) = basket.get(&fruit_kind) else {
     91                 panic!("Fruit kind {fruit_kind:?} was not found in basket");
     92             };
     93             assert!(*amount > 0);
     94         }
     95     }
     96 }