Ejercicios 46/94

This commit is contained in:
perro tuerto 2023-02-21 11:16:00 -08:00
parent 0cfdad15d9
commit 757596b997
6 changed files with 23 additions and 17 deletions

View File

@ -10,17 +10,18 @@
// //
// 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::new(); // TODO: declare your hash map here.
// 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 here. // TODO: Put more fruits in your basket here.
basket.insert(String::from("avocado"), 2);
basket.insert(String::from("tomato"), 1);
basket.insert(String::from("potato"), 1);
basket basket
} }

View File

@ -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,7 @@ 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!
basket.entry(fruit).or_insert(3);
} }
} }

View File

@ -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.
@ -40,6 +38,20 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded from team_2, and similarly // 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 // goals scored by team_2 will be the number of goals conceded by
// team_1. // team_1.
let team_1 = scores.entry(team_1_name.clone()).or_insert(Team {
name: team_1_name,
goals_scored: 0,
goals_conceded: 0,
});
team_1.goals_scored += team_1_score;
team_1.goals_conceded += team_2_score;
let team_2 = scores.entry(team_2_name.clone()).or_insert(Team {
name: team_2_name,
goals_scored: 0,
goals_conceded: 0,
});
team_2.goals_scored += team_2_score;
team_2.goals_conceded += team_1_score;
} }
scores scores
} }

View File

@ -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!");
} }

View File

@ -3,12 +3,10 @@
// '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 // TODO: Fix these use statements
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: &'static str = "Pear"; pub const PEAR: &'static str = "Pear";

View File

@ -5,10 +5,8 @@
// 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
// TODO: Complete this use statement // TODO: Complete this use statement
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) {