Skip to content

Commit

Permalink
finish hashmap && modules
Browse files Browse the repository at this point in the history
  • Loading branch information
yangrudan committed Nov 27, 2024
1 parent 78ea854 commit c18067c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
7 changes: 5 additions & 2 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
// hint.

// 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();// 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.
basket.insert("apple".to_string(), 3);
basket.insert("mango".to_string(), 2);
basket.insert("orange".into(), 5);
basket.insert("peach".into(), 4);

basket
}
Expand Down
4 changes: 3 additions & 1 deletion exercises/hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::collections::HashMap;

Expand All @@ -40,6 +39,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// TODO: Insert new fruits if they are not already present in the
// basket. 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);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion exercises/hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::collections::HashMap;

// A structure to store the goal details of a team.
#[derive(Default)]
struct Team {
goals_scored: u8,
goals_conceded: u8,
Expand All @@ -39,7 +39,12 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded from team_2, and similarly
// goals scored by team_2 will be the number of goals conceded by
// team_1.
scores.entry(team_1_name.clone()).or_default().goals_scored += team_1_score;
scores.entry(team_1_name.clone()).or_default().goals_conceded += team_2_score;
scores.entry(team_2_name.clone()).or_default().goals_scored += team_2_score;
scores.entry(team_2_name.clone()).or_default().goals_conceded += team_1_score;
}

scores
}

Expand Down
5 changes: 2 additions & 3 deletions exercises/modules/modules1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

mod sausage_factory {
pub mod sausage_factory {
// Don't let anybody outside of this module see this!
fn get_secret_recipe() -> String {
String::from("Ginger")
}

fn make_sausage() {
pub fn make_sausage() {
get_secret_recipe();
println!("sausage!");
}
Expand Down
8 changes: 4 additions & 4 deletions exercises/modules/modules2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[allow(dead_code)]
mod delicious_snacks {
// TODO: Fix these use statements
use self::fruits::PEAR as ???
use self::veggies::CUCUMBER as ???
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;

mod fruits {
pub const PEAR: &'static str = "Pear";
Expand All @@ -29,6 +29,6 @@ fn main() {
println!(
"favorite snacks: {} and {}",
delicious_snacks::fruit,
delicious_snacks::veggie
delicious_snacks::veggie,
);
}
3 changes: 1 addition & 2 deletions exercises/modules/modules3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

// TODO: Complete this use statement
use ???
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
match SystemTime::now().duration_since(UNIX_EPOCH) {
Expand Down
18 changes: 14 additions & 4 deletions exercises/quiz2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
//
// No hints this time!

// I AM NOT DONE

pub enum Command {
Uppercase,
Expand All @@ -32,11 +31,22 @@ mod my_module {
use super::Command;

// TODO: Complete the function signature!
pub fn transformer(input: ???) -> ??? {
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
// TODO: Complete the output declaration!
let mut output: ??? = vec![];
let mut output: Vec<String> = vec![];
for (string, command) in input.iter() {
// TODO: Complete the function body. You can do it!
match command {
Command::Uppercase => output.push(string.to_uppercase()),
Command::Trim => output.push(string.trim().to_string()),
Command::Append(n) => {
let mut tmp = string.clone();
for _a in 0..*n {
tmp.push_str("bar");
}
output.push(tmp);
}
}
}
output
}
Expand All @@ -45,7 +55,7 @@ mod my_module {
#[cfg(test)]
mod tests {
// TODO: What do we need to import to have `transformer` in scope?
use ???;
use super::my_module::transformer;
use super::Command;

#[test]
Expand Down

0 comments on commit c18067c

Please sign in to comment.