vec and hashmap1

This commit is contained in:
Chris Girvin 2022-05-18 10:14:29 -04:00
parent 2c55161c79
commit ad120c9547
3 changed files with 15 additions and 6 deletions

View File

@ -16,12 +16,24 @@
use std::collections::HashMap;
fn fruit_basket() -> HashMap<String, u32> {
let mut basket = // TODO: declare your hash map here.
let mut basket = HashMap::new(); // TODO: declare your hash map here.
// Two bananas are already given for you :)
basket.insert(String::from("banana"), 2);
// TODO: Put more fruits in your basket here.
// create fruit entries to add to basket
// iteratively or usion `Vec` method
let fruits = vec![
("apple".to_string(), 4),
("pear".to_string(), 1),
("peach".to_string(), 2),
];
// `extend` (or `append`) one array to the end of another
basket.extend(fruits);
// println!("{:?}", fruits);
basket
}

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]; // TODO: declare your vector here with the macro for vectors
(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 = *i * 2;
}
// At this point, `v` should be equal to [4, 8, 12, 16, 20].