hashmaps1.rs (1113B)
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 // 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 // Put more fruits in your basket. 17 basket.insert(String::from("apple"), 3); 18 basket.insert(String::from("mango"), 1); 19 20 basket 21 } 22 23 fn main() { 24 // You can optionally experiment here. 25 } 26 27 #[cfg(test)] 28 mod tests { 29 use super::*; 30 31 #[test] 32 fn at_least_three_types_of_fruits() { 33 let basket = fruit_basket(); 34 assert!(basket.len() >= 3); 35 } 36 37 #[test] 38 fn at_least_five_fruits() { 39 let basket = fruit_basket(); 40 assert!(basket.values().sum::<u32>() >= 5); 41 } 42 }