more exercise

This commit is contained in:
Tirth Patel 2024-03-11 00:14:10 -02:30
parent d9776cf9a7
commit c29bf477b7
15 changed files with 60 additions and 45 deletions

View File

@ -5,10 +5,12 @@
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
enum Message { enum Message {
// TODO: implement the message variant types based on their usage below ChangeColor(u8,u8,u8),
Echo(String),
Move(Point),
Quit
} }
struct Point { struct Point {
@ -44,6 +46,12 @@ impl State {
// TODO: create a match expression to process the different message variants // TODO: create a match expression to process the different message variants
// Remember: When passing a tuple as a function argument, you'll need extra parentheses: // Remember: When passing a tuple as a function argument, you'll need extra parentheses:
// fn function((t, u, p, l, e)) // fn function((t, u, p, l, e))
match message {
Message::ChangeColor(r,g,b) => self.color = (r,g,b),
Message::Echo(test) => self.message = test,
Message::Move(test) => self.position = test,
Message::Quit => self.quit = true
}
} }
} }

View File

@ -5,13 +5,12 @@
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a // Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
fn main() { fn main() {
let answer = current_favorite_color(); let answer = current_favorite_color();
println!("My current favorite color is {}", answer); println!("My current favorite color is {}", answer);
} }
fn current_favorite_color() -> String { fn current_favorite_color() -> &'static str {
"blue" "blue"
} }

View File

@ -5,7 +5,6 @@
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a // Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
fn main() { fn main() {
let word = String::from("green"); // Try not changing this line :) let word = String::from("green"); // Try not changing this line :)
@ -16,6 +15,6 @@ fn main() {
} }
} }
fn is_a_color_word(attempt: &str) -> bool { fn is_a_color_word(attempt: String) -> bool {
attempt == "green" || attempt == "blue" || attempt == "red" attempt == "green" || attempt == "blue" || attempt == "red"
} }

View File

@ -3,21 +3,21 @@
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a // Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
fn trim_me(input: &str) -> String { fn trim_me(input: &str) -> String {
// TODO: Remove whitespace from both ends of a string! // TODO: Remove whitespace from both ends of a string!
??? String::from(input.trim())
} }
fn compose_me(input: &str) -> String { fn compose_me(input: &str) -> String {
// TODO: Add " world!" to the string! There's multiple ways to do this! // TODO: Add " world!" to the string! There's multiple ways to do this!
??? let rv = String::from(input) + " world!";
rv
} }
fn replace_me(input: &str) -> String { fn replace_me(input: &str) -> String {
// TODO: Replace "cars" in the string with "balloons"! // TODO: Replace "cars" in the string with "balloons"!
??? input.replace("cars","balloons")
} }
#[cfg(test)] #[cfg(test)]

View File

@ -7,7 +7,6 @@
// //
// No hints this time! // No hints this time!
// I AM NOT DONE
fn string_slice(arg: &str) { fn string_slice(arg: &str) {
println!("{}", arg); println!("{}", arg);
@ -17,14 +16,14 @@ fn string(arg: String) {
} }
fn main() { fn main() {
???("blue"); string_slice("blue");
???("red".to_string()); string("red".to_string());
???(String::from("hi")); string(String::from("hi"));
???("rust is fun!".to_owned()); string("rust is fun!".to_owned());
???("nice weather".into()); string("nice weather".into());
???(format!("Interpolation {}", "Station")); string(format!("Interpolation {}", "Station"));
???(&String::from("abc")[0..1]); string_slice(&String::from("abc")[0..1]);
???(" hello there ".trim()); string_slice(" hello there ".trim());
???("Happy Monday!".to_string().replace("Mon", "Tues")); string("Happy Monday!".to_string().replace("Mon", "Tues"));
???("mY sHiFt KeY iS sTiCkY".to_lowercase()); string("mY sHiFt KeY iS sTiCkY".to_lowercase());
} }

View File

@ -3,7 +3,6 @@
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a // Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
// hint. // 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!
@ -11,7 +10,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!");
} }

View File

@ -7,12 +7,11 @@
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a // Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
// hint. // 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

@ -8,10 +8,9 @@
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a // Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
// hint. // 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) {

View File

@ -11,18 +11,18 @@
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
// hint. // 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("apple"), 2);
basket.insert(String::from("gum-gum"), 2);
basket basket
} }

View File

@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
@ -40,6 +39,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// TODO: Insert new fruits if they are not already present in the // TODO: Insert new fruits if they are not already present in the
// basket. Note that you are not allowed to put any type of fruit that's // basket. Note that you are not allowed to put any type of fruit that's
// already present! // already present!
if !basket.contains_key(&fruit) {
basket.insert(fruit, 2);
}
} }
} }

View File

@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
@ -39,6 +38,12 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded by team_2, and similarly // will be the number of goals conceded by 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.
scores.entry(team_1_name)
.and_modify(|team| {team.goals_scored = team.goals_scored + team_1_score; team.goals_conceded= team.goals_conceded + team_2_score;})
.or_insert(Team {goals_scored: team_1_score, goals_conceded: team_2_score});
scores.entry(team_2_name)
.and_modify(|team| {team.goals_scored= team.goals_scored + team_2_score; team.goals_conceded= team.goals_conceded + team_1_score;})
.or_insert(Team {goals_scored: team_2_score, goals_conceded: team_1_score});
} }
scores scores
} }

View File

@ -3,7 +3,6 @@
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a // Execute `rustlings hint options1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
// This function returns how much icecream there is left in the fridge. // This function returns how much icecream there is left in the fridge.
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them // If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
@ -13,7 +12,12 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// value of 0 The Option output should gracefully handle cases where // value of 0 The Option output should gracefully handle cases where
// time_of_day > 23. // time_of_day > 23.
// TODO: Complete the function body - remember to return an Option! // TODO: Complete the function body - remember to return an Option!
??? if time_of_day < 22 {
return Some(5)
} else if time_of_day > 21 && time_of_day < 24 {
return Some(0)
}
return None
} }
#[cfg(test)] #[cfg(test)]
@ -34,6 +38,6 @@ mod tests {
// TODO: Fix this test. How do you get at the value contained in the // TODO: Fix this test. How do you get at the value contained in the
// Option? // Option?
let icecreams = maybe_icecream(12); let icecreams = maybe_icecream(12);
assert_eq!(icecreams, 5); assert_eq!(icecreams, Some(5));
} }
} }

View File

@ -3,7 +3,6 @@
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a // Execute `rustlings hint options2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -13,7 +12,7 @@ mod tests {
let optional_target = Some(target); let optional_target = Some(target);
// TODO: Make this an if let statement whose value is "Some" type // TODO: Make this an if let statement whose value is "Some" type
word = optional_target { if let Some(word) = optional_target {
assert_eq!(word, target); assert_eq!(word, target);
} }
} }
@ -32,7 +31,7 @@ mod tests {
// TODO: make this a while let statement - remember that vector.pop also // TODO: make this a while let statement - remember that vector.pop also
// adds another layer of Option<T>. You can stack `Option<T>`s into // adds another layer of Option<T>. You can stack `Option<T>`s into
// while let and if let. // while let and if let.
integer = optional_integers.pop() { while let Some(Some(integer)) = optional_integers.pop() {
assert_eq!(integer, cursor); assert_eq!(integer, cursor);
cursor -= 1; cursor -= 1;
} }

View File

@ -3,7 +3,6 @@
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a // Execute `rustlings hint options3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
struct Point { struct Point {
x: i32, x: i32,
@ -13,7 +12,7 @@ struct Point {
fn main() { fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 }); let y: Option<Point> = Some(Point { x: 100, y: 200 });
match y { match &y {
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => panic!("no match!"), _ => panic!("no match!"),
} }

View File

@ -20,7 +20,6 @@
// //
// No hints this time! // No hints this time!
// I AM NOT DONE
pub enum Command { pub enum Command {
Uppercase, Uppercase,
@ -32,10 +31,15 @@ mod my_module {
use super::Command; use super::Command;
// TODO: Complete the function signature! // TODO: Complete the function signature!
pub fn transformer(input: ???) -> ??? { pub fn transformer(input: Vec<(String,Command)>) -> Vec<String> {
// 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() {
output.push(match command {
Command::Uppercase => string.to_uppercase().to_string(),
Command::Trim => string.trim().to_string(),
Command::Append(s) => string.to_string() + &"bar".repeat(*s)
});
// TODO: Complete the function body. You can do it! // TODO: Complete the function body. You can do it!
} }
output output
@ -45,7 +49,7 @@ mod my_module {
#[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]