Solve collection exercises.

This commit is contained in:
Leonardo Freua 2021-09-14 10:19:23 -03:00
parent e730260ad4
commit 6611ddff66
4 changed files with 8 additions and 10 deletions

View File

@ -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<String, u32> {
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
}

View File

@ -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<Fruit, u32>) {
// 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);
}
}
}

View File

@ -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<i32>) {
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)
}

View File

@ -7,12 +7,11 @@
// Execute the command `rustlings hint vec2` if you need
// hints.
// I AM NOT DONE
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
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].