rustlings

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

hashmaps2.rs (2893B)


      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         // TODO: Insert new fruits if they are not already present in the
     33         // basket. Note that you are not allowed to put any type of fruit that's
     34         // already present!
     35         basket.entry(fruit).or_insert(1);
     36     }
     37 }
     38 
     39 fn main() {
     40     // You can optionally experiment here.
     41 }
     42 
     43 #[cfg(test)]
     44 mod tests {
     45     use super::*;
     46 
     47     // Don't modify this function!
     48     fn get_fruit_basket() -> HashMap<Fruit, u32> {
     49         let content = [(Fruit::Apple, 4), (Fruit::Mango, 2), (Fruit::Lychee, 5)];
     50         HashMap::from_iter(content)
     51     }
     52 
     53     #[test]
     54     fn test_given_fruits_are_not_modified() {
     55         let mut basket = get_fruit_basket();
     56         fruit_basket(&mut basket);
     57         assert_eq!(*basket.get(&Fruit::Apple).unwrap(), 4);
     58         assert_eq!(*basket.get(&Fruit::Mango).unwrap(), 2);
     59         assert_eq!(*basket.get(&Fruit::Lychee).unwrap(), 5);
     60     }
     61 
     62     #[test]
     63     fn at_least_five_types_of_fruits() {
     64         let mut basket = get_fruit_basket();
     65         fruit_basket(&mut basket);
     66         let count_fruit_kinds = basket.len();
     67         assert!(count_fruit_kinds >= 5);
     68     }
     69 
     70     #[test]
     71     fn greater_than_eleven_fruits() {
     72         let mut basket = get_fruit_basket();
     73         fruit_basket(&mut basket);
     74         let count = basket.values().sum::<u32>();
     75         assert!(count > 11);
     76     }
     77 
     78     #[test]
     79     fn all_fruit_types_in_basket() {
     80         let fruit_kinds = [
     81             Fruit::Apple,
     82             Fruit::Banana,
     83             Fruit::Mango,
     84             Fruit::Lychee,
     85             Fruit::Pineapple,
     86         ];
     87 
     88         let mut basket = get_fruit_basket();
     89         fruit_basket(&mut basket);
     90 
     91         for fruit_kind in fruit_kinds {
     92             let Some(amount) = basket.get(&fruit_kind) else {
     93                 panic!("Fruit kind {fruit_kind:?} was not found in basket");
     94             };
     95             assert!(*amount > 0);
     96         }
     97     }
     98 }