hashmaps1.rs (1216B)
1 // A basket of fruits in the form of a hash map needs to be defined. The key 2 // represents the name of the fruit and the value represents how many of that 3 // particular fruit is in the basket. You have to put at least 3 different 4 // types of fruits (e.g. apple, banana, mango) in the basket and the total count 5 // of all the fruits should be at least 5. 6 7 use std::collections::HashMap; 8 9 fn fruit_basket() -> HashMap<String, u32> { 10 // TODO: Declare the hash map. 11 let mut basket = HashMap::new(); 12 13 // Two bananas are already given for you :) 14 basket.insert(String::from("banana"), 2); 15 16 // TODO: Put more fruits in your basket. 17 basket.insert(String::from("orange"), 2); 18 basket.insert(String::from("apple"), 1); 19 basket.insert(String::from("apple"), 1); 20 basket.insert(String::from("apple"), 1); 21 basket 22 } 23 24 fn main() { 25 // You can optionally experiment here. 26 } 27 28 #[cfg(test)] 29 mod tests { 30 use super::*; 31 32 #[test] 33 fn at_least_three_types_of_fruits() { 34 let basket = fruit_basket(); 35 assert!(basket.len() >= 3); 36 } 37 38 #[test] 39 fn at_least_five_fruits() { 40 let basket = fruit_basket(); 41 assert!(basket.values().sum::<u32>() >= 5); 42 } 43 }