mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 21:59:18 +00:00
hashmaps, modules, and quiz #2
This commit is contained in:
parent
869bb96633
commit
50adf98d5a
@ -10,15 +10,15 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// 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.
|
let mut basket: HashMap<String, u32> = HashMap::new();
|
||||||
|
|
||||||
// Two bananas are already given for you :)
|
// Two bananas are already given for you :)
|
||||||
basket.insert(String::from("banana"), 2);
|
basket.insert(String::from("banana"), 2);
|
||||||
|
basket.insert(String::from("apple"), 3);
|
||||||
|
basket.insert(String::from("mango"), 4);
|
||||||
|
|
||||||
// TODO: Put more fruits in your basket here.
|
// TODO: Put more fruits in your basket here.
|
||||||
|
|
||||||
|
|||||||
@ -11,8 +11,6 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Hash, PartialEq, Eq)]
|
#[derive(Hash, PartialEq, Eq)]
|
||||||
@ -37,6 +35,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
|||||||
// TODO: Put new fruits if not already present. Note that you
|
// TODO: Put new fruits if not already present. Note that you
|
||||||
// are not allowed to put any type of fruit that's already
|
// are not allowed to put any type of fruit that's already
|
||||||
// present!
|
// present!
|
||||||
|
if !basket.contains_key(&fruit) {
|
||||||
|
basket.insert(fruit, 5);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,8 +14,6 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// A structure to store team name and its goal details.
|
// A structure to store team name and its goal details.
|
||||||
@ -35,11 +33,36 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
|||||||
let team_1_score: u8 = v[2].parse().unwrap();
|
let team_1_score: u8 = v[2].parse().unwrap();
|
||||||
let team_2_name = v[1].to_string();
|
let team_2_name = v[1].to_string();
|
||||||
let team_2_score: u8 = v[3].parse().unwrap();
|
let team_2_score: u8 = v[3].parse().unwrap();
|
||||||
// TODO: Populate the scores table with details extracted from the
|
|
||||||
// current line. Keep in mind that goals scored by team_1
|
let team_1_updated_status = match scores.get(&team_1_name) {
|
||||||
// will be number of goals conceded from team_2, and similarly
|
Some(team) => Team {
|
||||||
// goals scored by team_2 will be the number of goals conceded by
|
name: team_1_name,
|
||||||
// team_1.
|
goals_scored: team.goals_scored + team_1_score,
|
||||||
|
goals_conceded: team.goals_conceded + team_2_score,
|
||||||
|
},
|
||||||
|
None => Team {
|
||||||
|
name: team_1_name,
|
||||||
|
goals_scored: team_1_score,
|
||||||
|
goals_conceded: team_2_score,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
scores.insert(team_1_updated_status.name.clone(), team_1_updated_status);
|
||||||
|
|
||||||
|
let team_2_updated_status = match scores.get(&team_2_name) {
|
||||||
|
Some(team) => Team {
|
||||||
|
name: team_2_name,
|
||||||
|
goals_scored: team.goals_scored + team_2_score,
|
||||||
|
goals_conceded: team.goals_conceded + team_1_score,
|
||||||
|
},
|
||||||
|
None => Team {
|
||||||
|
name: team_2_name,
|
||||||
|
goals_scored: team_2_score,
|
||||||
|
goals_conceded: team_1_score,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
scores.insert(team_2_updated_status.name.clone(), team_2_updated_status);
|
||||||
}
|
}
|
||||||
scores
|
scores
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,13 @@
|
|||||||
// modules1.rs
|
// modules1.rs
|
||||||
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
mod sausage_factory {
|
mod sausage_factory {
|
||||||
// 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() {
|
||||||
get_secret_recipe();
|
get_secret_recipe();
|
||||||
println!("sausage!");
|
println!("sausage!");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,12 +3,9 @@
|
|||||||
// '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.
|
||||||
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
mod delicious_snacks {
|
mod delicious_snacks {
|
||||||
// TODO: Fix these use statements
|
pub use self::fruits::PEAR as fruit;
|
||||||
use self::fruits::PEAR as ???
|
pub use self::veggies::CUCUMBER as veggie;
|
||||||
use self::veggies::CUCUMBER as ???
|
|
||||||
|
|
||||||
mod fruits {
|
mod fruits {
|
||||||
pub const PEAR: &'static str = "Pear";
|
pub const PEAR: &'static str = "Pear";
|
||||||
|
|||||||
@ -5,10 +5,7 @@
|
|||||||
// 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!
|
||||||
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
// TODO: Complete this use statement
|
|
||||||
use ???
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||||
|
|||||||
@ -18,8 +18,6 @@
|
|||||||
// - The output element is going to be a Vector of strings.
|
// - The output element is going to be a Vector of strings.
|
||||||
// Execute `rustlings hint quiz2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint quiz2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
Uppercase,
|
Uppercase,
|
||||||
Trim,
|
Trim,
|
||||||
@ -29,12 +27,15 @@ pub enum Command {
|
|||||||
mod my_module {
|
mod my_module {
|
||||||
use super::Command;
|
use super::Command;
|
||||||
|
|
||||||
// TODO: Complete the function signature!
|
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
||||||
pub fn transformer(input: ???) -> ??? {
|
|
||||||
// TODO: Complete the output declaration!
|
// TODO: Complete the output declaration!
|
||||||
let mut output: ??? = vec![];
|
let mut output: Vec<String> = vec![];
|
||||||
for (string, command) in input.iter() {
|
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(size) => output.push(string.to_owned() + &"bar".repeat(*size)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
@ -42,8 +43,7 @@ mod my_module {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
// TODO: What do we have to import to have `transformer` in scope?
|
use super::my_module::transformer;
|
||||||
use ???;
|
|
||||||
use super::Command;
|
use super::Command;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user