mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-01 00:09:18 +00:00
quiz2
This commit is contained in:
parent
512d418f7f
commit
deadba31d5
@ -5,7 +5,7 @@ mod sausage_factory {
|
|||||||
String::from("Ginger")
|
String::from("Ginger")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_sausage() {
|
pub fn make_sausage() {
|
||||||
get_secret_recipe();
|
get_secret_recipe();
|
||||||
println!("sausage!");
|
println!("sausage!");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,8 @@
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
mod delicious_snacks {
|
mod delicious_snacks {
|
||||||
// TODO: Add the following two `use` statements after fixing them.
|
// TODO: Add the following two `use` statements after fixing them.
|
||||||
// use self::fruits::PEAR as ???;
|
pub use self::fruits::PEAR as fruit;
|
||||||
// use self::veggies::CUCUMBER as ???;
|
pub use self::veggies::CUCUMBER as veggie;
|
||||||
|
|
||||||
mod fruits {
|
mod fruits {
|
||||||
pub const PEAR: &str = "Pear";
|
pub const PEAR: &str = "Pear";
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
// TODO: Bring `SystemTime` and `UNIX_EPOCH` from the `std::time` module into
|
// TODO: Bring `SystemTime` and `UNIX_EPOCH` from the `std::time` module into
|
||||||
// your scope. Bonus style points if you can do it with one line!
|
// your scope. Bonus style points if you can do it with one line!
|
||||||
// use ???;
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||||
|
|||||||
@ -8,12 +8,14 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
fn fruit_basket() -> HashMap<String, u32> {
|
fn fruit_basket() -> HashMap<String, u32> {
|
||||||
// TODO: Declare the hash map.
|
// TODO: Declare the hash map.
|
||||||
// let mut basket =
|
let mut basket = 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);
|
||||||
|
|
||||||
// TODO: Put more fruits in your basket.
|
// TODO: Put more fruits in your basket.
|
||||||
|
basket.insert(String::from("apple"), 3);
|
||||||
|
basket.insert(String::from("watermelon"), 1);
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,9 +29,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for fruit in fruit_kinds {
|
for fruit in fruit_kinds {
|
||||||
// TODO: Insert new fruits if they are not already present in the
|
basket.entry(fruit).or_insert(1);
|
||||||
// basket. Note that you are not allowed to put any type of fruit that's
|
|
||||||
// already present!
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -27,10 +27,12 @@ fn build_scores_table(results: &str) -> HashMap<&str, Team> {
|
|||||||
let team_1_score: u8 = split_iterator.next().unwrap().parse().unwrap();
|
let team_1_score: u8 = split_iterator.next().unwrap().parse().unwrap();
|
||||||
let team_2_score: u8 = split_iterator.next().unwrap().parse().unwrap();
|
let team_2_score: u8 = split_iterator.next().unwrap().parse().unwrap();
|
||||||
|
|
||||||
// TODO: Populate the scores table with the extracted details.
|
let entry1 = scores.entry(team_1_name).or_insert(Team { goals_scored: 0, goals_conceded: 0 });
|
||||||
// Keep in mind that goals scored by team 1 will be the number of goals
|
entry1.goals_scored += team_1_score;
|
||||||
// conceded by team 2. Similarly, goals scored by team 2 will be the
|
entry1.goals_conceded += team_2_score;
|
||||||
// number of goals conceded by team 1.
|
let entry2 = scores.entry(team_2_name).or_insert(Team { goals_scored: 0, goals_conceded: 0 });
|
||||||
|
entry2.goals_scored += team_2_score;
|
||||||
|
entry2.goals_conceded += team_1_score;
|
||||||
}
|
}
|
||||||
|
|
||||||
scores
|
scores
|
||||||
|
|||||||
@ -27,7 +27,21 @@ mod my_module {
|
|||||||
use super::Command;
|
use super::Command;
|
||||||
|
|
||||||
// TODO: Complete the function.
|
// TODO: Complete the function.
|
||||||
// pub fn transformer(input: ???) -> ??? { ??? }
|
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
||||||
|
input.iter().map(|(item, cmd)| {
|
||||||
|
match cmd {
|
||||||
|
Command::Uppercase => {
|
||||||
|
item.to_uppercase()
|
||||||
|
}
|
||||||
|
Command::Trim => {
|
||||||
|
item.trim().to_string()
|
||||||
|
}
|
||||||
|
Command::Append(size) => {
|
||||||
|
format!("{}{}", item, "bar".repeat(*size))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -37,7 +51,7 @@ fn main() {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
// TODO: What do we need to import to have `transformer` in scope?
|
// TODO: What do we need to import to have `transformer` in scope?
|
||||||
// use ???;
|
use super::my_module::{transformer};
|
||||||
use super::Command;
|
use super::Command;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user