Starting on hashmaps.

This commit is contained in:
Justin Kelz 2022-03-26 13:08:19 -07:00
parent 892f693cfd
commit d870e3c944
7 changed files with 29 additions and 21 deletions

View File

@ -11,17 +11,19 @@
// Execute the command `rustlings hint hashmap1` if you need // Execute the command `rustlings hint hashmap1` if you need
// hints. // hints.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
fn fruit_basket() -> HashMap<String, u32> { fn fruit_basket() -> HashMap<String, u32> {
let mut basket = // TODO: declare your hash map here. // 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. // TODO: Put more fruits in your basket here.
// Two bananas are already given for you :)
// Format is basket.insert(String,u32);
basket.insert(String::from("banana"), 2);
basket.insert(String::from("bapple"),4);
basket.insert(String::from("horange"),1);
basket basket
} }

View File

@ -4,8 +4,9 @@
// represents the name of the fruit and the value represents how many // represents the name of the fruit and the value represents how many
// of that particular fruit is in the basket. You have to put *MORE // of that particular fruit is in the basket. You have to put *MORE
// THAN 11* fruits in the basket. Three types of fruits - Apple (4), // THAN 11* fruits in the basket. Three types of fruits - Apple (4),
// Mango (2) and Lychee (5) are already given in the basket. You are // Mango (2) and Lychee (5) are already given in the basket.
// not allowed to insert any more of these fruits! //
// You are not allowed to insert any more of these fruits!
// //
// Make me pass the tests! // Make me pass the tests!
// //

View File

@ -4,11 +4,14 @@
// Make me compile and pass the test! // Make me compile and pass the test!
// Execute the command `rustlings hint vec1` if you need hints. // Execute the command `rustlings hint vec1` if you need hints.
// I AM NOT DONE
fn array_and_vec() -> ([i32; 4], Vec<i32>) { fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array let a = [10, 20, 30, 40]; // a plain array
let v = // TODO: declare your vector here with the macro for vectors // TODO: declare your vector here with the macro for vectors
let mut v: Vec<i32> = Vec::new(); // make v a mutable vector!
for i in a.iter() { // making at iterable
v.push(*i) // leting i establish the expected type here
}
(a, v) (a, v)
} }

View File

@ -7,12 +7,15 @@
// Execute the command `rustlings hint vec2` if you need // Execute the command `rustlings hint vec2` if you need
// hints. // hints.
// I AM NOT DONE
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> { fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for i in v.iter_mut() { for i in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is // TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2. // multiplied by 2.
// not so obvious answer, this is because multiplication is a right action in Rust <20><>
*i = *i*2;
// slick answer below
//*i *= 2; // pythonish: i = 2*i
} }
// At this point, `v` should be equal to [4, 8, 12, 16, 20]. // At this point, `v` should be equal to [4, 8, 12, 16, 20].

View File

@ -1,15 +1,14 @@
// modules1.rs // modules1.rs
// Make me compile! Execute `rustlings hint modules1` for hints :) // Make me compile! Execute `rustlings hint modules1` for hints :)
// I AM NOT DONE
mod sausage_factory { mod sausage_factory { // adding pub here doesn't seem to matter . . . If I had to guess, moduless default to public.
// Don't let anybody outside of this module see this! // Don't let anybody outside of this module see this!
fn get_secret_recipe() -> String { fn get_secret_recipe() -> String {
String::from("Ginger") String::from("Ginger")
} }
fn make_sausage() { pub fn make_sausage() { // adding pub here fixes the problem. I think the lesson is that functions are inherely private, and have to be explicitly made public?
get_secret_recipe(); get_secret_recipe();
println!("sausage!"); println!("sausage!");
} }

View File

@ -3,13 +3,12 @@
// 'use' and 'as' keywords. Fix these 'use' statements to make the code compile. // 'use' and 'as' keywords. Fix these 'use' statements to make the code compile.
// Make me compile! Execute `rustlings hint modules2` for hints :) // Make me compile! Execute `rustlings hint modules2` for hints :)
// I AM NOT DONE
mod delicious_snacks { mod delicious_snacks {
// TODO: Fix these use statements // TODO: Fix these use statements
use self::fruits::PEAR as ??? pub use self::fruits::PEAR as fruit; // so I'm telling this that PEAR is a fruit.
use self::veggies::CUCUMBER as ??? pub use self::veggies::CUCUMBER as veggie; // and here I'm saying CUCUMBER is a veggie.
mod fruits { mod fruits {
pub const PEAR: &'static str = "Pear"; pub const PEAR: &'static str = "Pear";
@ -25,7 +24,7 @@ mod delicious_snacks {
fn main() { fn main() {
println!( println!(
"favorite snacks: {} and {}", "favorite snacks: {} and {}",
delicious_snacks::fruit, delicious_snacks::fruit, // So we're looking for a delicious snack, but it has to be fruit.
delicious_snacks::veggie delicious_snacks::veggie // and here it needs to be veggie
); );
} }

View File

@ -5,10 +5,11 @@
// from the std::time module. Bonus style points if you can do it with one line! // from the std::time module. Bonus style points if you can do it with one line!
// Make me compile! Execute `rustlings hint modules3` for hints :) // Make me compile! Execute `rustlings hint modules3` for hints :)
// I AM NOT DONE
// TODO: Complete this use statement // TODO: Complete this use statement
use ??? use std::time::{UNIX_EPOCH,SystemTime}; // Hey, I did it!
// pub use std::time::SystemTime as SystemTime; // This is redundant, you just need everything before as
// pub use std::time::UNIX_EPOCH as UNIX_EPOCH;
fn main() { fn main() {
match SystemTime::now().duration_since(UNIX_EPOCH) { match SystemTime::now().duration_since(UNIX_EPOCH) {