From 48f4ea1a66f9d991876fccd5cfb3b4c8980928bb Mon Sep 17 00:00:00 2001 From: Leonardo Freua Date: Tue, 14 Sep 2021 10:19:23 -0300 Subject: [PATCH] Solve collection exercises. --- exercises/collections/hashmap1.rs | 6 +++--- exercises/collections/hashmap2.rs | 5 +++-- exercises/collections/vec1.rs | 4 +--- exercises/collections/vec2.rs | 3 +-- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/exercises/collections/hashmap1.rs b/exercises/collections/hashmap1.rs index 64b5a7f3..b4226e5e 100644 --- a/exercises/collections/hashmap1.rs +++ b/exercises/collections/hashmap1.rs @@ -11,17 +11,17 @@ // Execute the command `rustlings hint hashmap1` if you need // hints. -// I AM NOT DONE - use std::collections::HashMap; fn fruit_basket() -> HashMap { - let mut basket = // TODO: declare your hash map here. + let mut basket = HashMap::new(); // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); // TODO: Put more fruits in your basket here. + basket.insert(String::from("apple"), 1); + basket.insert(String::from("mango"), 2); basket } diff --git a/exercises/collections/hashmap2.rs b/exercises/collections/hashmap2.rs index 0abe19ab..849b00a9 100644 --- a/exercises/collections/hashmap2.rs +++ b/exercises/collections/hashmap2.rs @@ -12,8 +12,6 @@ // Execute the command `rustlings hint hashmap2` if you need // hints. -// I AM NOT DONE - use std::collections::HashMap; #[derive(Hash, PartialEq, Eq)] @@ -38,6 +36,9 @@ fn fruit_basket(basket: &mut HashMap) { // TODO: Put new fruits if not already present. Note that you // are not allowed to put any type of fruit that's already // present! + if basket.get(&fruit) == None { + basket.insert(fruit, 1); + } } } diff --git a/exercises/collections/vec1.rs b/exercises/collections/vec1.rs index b144fb94..7db63d85 100644 --- a/exercises/collections/vec1.rs +++ b/exercises/collections/vec1.rs @@ -4,11 +4,9 @@ // Make me compile and pass the test! // Execute the command `rustlings hint vec1` if you need hints. -// I AM NOT DONE - fn array_and_vec() -> ([i32; 4], Vec) { let a = [10, 20, 30, 40]; // a plain array - let v = // TODO: declare your vector here with the macro for vectors + let v = vec![10, 20, 30, 40]; (a, v) } diff --git a/exercises/collections/vec2.rs b/exercises/collections/vec2.rs index 6595e401..436f2ce7 100644 --- a/exercises/collections/vec2.rs +++ b/exercises/collections/vec2.rs @@ -7,12 +7,11 @@ // Execute the command `rustlings hint vec2` if you need // hints. -// I AM NOT DONE - fn vec_loop(mut v: Vec) -> Vec { for i in v.iter_mut() { // TODO: Fill this up so that each element in the Vec `v` is // multiplied by 2. + *i *= 2; } // At this point, `v` should be equal to [4, 8, 12, 16, 20].