mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29: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.
|
||||
|
||||
// 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<String, u32> = HashMap::new();
|
||||
|
||||
// Two bananas are already given for you :)
|
||||
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.
|
||||
|
||||
|
||||
@ -11,8 +11,6 @@
|
||||
//
|
||||
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[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
|
||||
// are not allowed to put any type of fruit that's already
|
||||
// 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.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
// 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_2_name = v[1].to_string();
|
||||
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
|
||||
// will be number of goals conceded from team_2, and similarly
|
||||
// goals scored by team_2 will be the number of goals conceded by
|
||||
// team_1.
|
||||
|
||||
let team_1_updated_status = match scores.get(&team_1_name) {
|
||||
Some(team) => Team {
|
||||
name: team_1_name,
|
||||
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
|
||||
}
|
||||
|
||||
@ -1,15 +1,13 @@
|
||||
// modules1.rs
|
||||
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
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!");
|
||||
}
|
||||
|
||||
@ -3,12 +3,9 @@
|
||||
// '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.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
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";
|
||||
|
||||
@ -5,10 +5,7 @@
|
||||
// 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.
|
||||
|
||||
// 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) {
|
||||
|
||||
@ -18,8 +18,6 @@
|
||||
// - The output element is going to be a Vector of strings.
|
||||
// Execute `rustlings hint quiz2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub enum Command {
|
||||
Uppercase,
|
||||
Trim,
|
||||
@ -29,12 +27,15 @@ pub enum Command {
|
||||
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(size) => output.push(string.to_owned() + &"bar".repeat(*size)),
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
@ -42,8 +43,7 @@ mod my_module {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// TODO: What do we have to import to have `transformer` in scope?
|
||||
use ???;
|
||||
use super::my_module::transformer;
|
||||
use super::Command;
|
||||
|
||||
#[test]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user