From 8fa32768ae2ad4d12bc4a6ff4ccb18092c1e88eb Mon Sep 17 00:00:00 2001 From: NoneTheWisr <12655815+NoneTheWisr@users.noreply.github.com> Date: Fri, 28 Jan 2022 18:36:19 +0400 Subject: [PATCH] finish collections --- exercises/collections/hashmap1.rs | 8 ++++---- exercises/collections/hashmap2.rs | 8 +++----- exercises/collections/vec1.rs | 4 +--- exercises/collections/vec2.rs | 5 +---- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/exercises/collections/hashmap1.rs b/exercises/collections/hashmap1.rs index 64b5a7f3..7a1cb2e4 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"), 2); + basket.insert(String::from("mango"), 1); basket } diff --git a/exercises/collections/hashmap2.rs b/exercises/collections/hashmap2.rs index 0abe19ab..6d3f5fc9 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)] @@ -35,9 +33,9 @@ fn fruit_basket(basket: &mut HashMap) { ]; for fruit in fruit_kinds { - // 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.contains_key(&fruit) { + basket.insert(fruit, 1); + } } } diff --git a/exercises/collections/vec1.rs b/exercises/collections/vec1.rs index b144fb94..476e6ae5 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 vector (a, v) } diff --git a/exercises/collections/vec2.rs b/exercises/collections/vec2.rs index 6595e401..2b85627b 100644 --- a/exercises/collections/vec2.rs +++ b/exercises/collections/vec2.rs @@ -7,12 +7,9 @@ // 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].