done till exercises/options/options1

This commit is contained in:
ben1009 2023-02-15 18:41:14 +08:00
parent 992ea90078
commit a4f9fc297a
9 changed files with 61 additions and 18 deletions

View File

@ -1,4 +1,5 @@
# Hashmaps
A *hash map* allows you to associate a value with a particular key.
You may also know this by the names [*unordered map* in C++](https://en.cppreference.com/w/cpp/container/unordered_map),
[*dictionary* in Python](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) or an *associative array* in other languages.

View File

@ -10,17 +10,18 @@
//
// 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(String::from("apple"), 3);
basket.insert(String::from("apple1"), 3);
basket
}

View File

@ -11,7 +11,6 @@
//
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::collections::HashMap;
@ -37,6 +36,7 @@ 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!
basket.entry(fruit).or_insert(1);
}
}

View File

@ -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.
@ -40,6 +38,42 @@ 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.
let team1 = scores.get_mut(&team_1_name);
match team1 {
Some(t) => {
t.goals_scored += team_1_score;
t.goals_conceded += team_2_score;
}
None => {
scores.insert(
team_1_name.clone(),
Team {
name: team_1_name.clone(),
goals_scored: team_1_score,
goals_conceded: team_2_score,
},
);
}
}
let team2 = scores.get_mut(&team_2_name);
match team2 {
Some(t) => {
t.goals_scored += team_2_score;
t.goals_conceded += team_1_score;
}
None => {
scores.insert(
team_2_name.clone(),
Team {
name: team_2_name.clone(),
goals_scored: team_2_score,
goals_conceded: team_1_score,
},
);
}
}
}
scores
}

View File

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

View File

@ -3,12 +3,11 @@
// '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";

View File

@ -5,10 +5,10 @@
// 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) {

View File

@ -18,8 +18,6 @@
// - The output element is going to be a Vector of strings.
// No hints this time!
// I AM NOT DONE
pub enum Command {
Uppercase,
Trim,
@ -30,11 +28,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![];
for (string, command) in input.iter() {
// TODO: Complete the function body. You can do it!
match command {
Command::Append(u) => {
output.push(format!("{}{}", string, "bar".repeat(*u)));
}
Command::Trim => {
output.push(string.trim().to_string());
}
Command::Uppercase => {
output.push(string.to_uppercase().to_string());
}
}
}
output
}
@ -43,7 +52,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]

View File

@ -9,6 +9,7 @@
fn string_slice(arg: &str) {
println!("{}", arg);
}
fn string(arg: String) {
println!("{}", arg);
}