Skip to content

Commit

Permalink
finish struct enum string module hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-jw committed Jan 15, 2025
1 parent 106aa0b commit 29dc22c
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 55 deletions.
5 changes: 4 additions & 1 deletion exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
//
// No hints this time! ;)

// I AM NOT DONE

#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor
}

fn main() {
Expand Down
5 changes: 4 additions & 1 deletion exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Move{x:i32, y:i32},
Echo(String),
ChangeColor(u32, u32, u32),
Quit
}

impl Message {
Expand Down
14 changes: 11 additions & 3 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

enum Message {
// TODO: implement the message variant types based on their usage below
// TODO: define the different variants used below
Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
Quit
}

struct Point {
Expand Down Expand Up @@ -43,6 +45,12 @@ impl State {
// variants
// Remember: When passing a tuple as a function argument, you'll need
// extra parentheses: fn function((t, u, p, l, e))
match message {
Message::ChangeColor(r, g, b) => { self.change_color((r, g, b));}
Message::Echo(string) => { self.echo(string);}
Message::Move(p) => {self.move_position(Point{x:p.x, y:p.y});}
Message::Quit =>{self.quit();}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
// 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("apple".to_string(), 2);
basket.insert("pear".to_string(), 2);

basket
}
Expand Down
5 changes: 3 additions & 2 deletions exercises/hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,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)]
Expand All @@ -40,6 +38,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// 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
// already present!
if basket.get(&fruit) == None {
basket.insert(fruit, 1);
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions exercises/hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 the goal details of a team.
Expand All @@ -34,6 +32,20 @@ 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();
if let Some(mut t1_goal_scored )= scores.get_mut(&team_1_name) {
t1_goal_scored.goals_scored += team_1_score;
t1_goal_scored.goals_conceded += team_1_score;
}
else {
scores.insert(team_1_name, Team{goals_scored: team_1_score, goals_conceded: team_2_score});
}
if let Some(mut t2) = scores.get_mut(&team_2_name) {
t2.goals_scored += team_2_score;
t2.goals_conceded += team_1_score;
}
else {
scores.insert(team_2_name, Team{goals_scored: team_2_score, goals_conceded: team_1_score});
}
// TODO: Populate the scores table with details extracted from the
// current line. Keep in mind that goals scored by team_1
// will be the number of goals conceded from team_2, and similarly
Expand Down
4 changes: 1 addition & 3 deletions exercises/modules/modules1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
// 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!");
}
Expand Down
6 changes: 2 additions & 4 deletions exercises/modules/modules2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
// 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";
Expand Down
4 changes: 1 addition & 3 deletions exercises/modules/modules3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
// 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) {
Expand Down
4 changes: 1 addition & 3 deletions exercises/strings/strings1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn main() {
let answer = current_favorite_color();
println!("My current favorite color is {}", answer);
}

fn current_favorite_color() -> String {
"blue"
String::from("blue")
}
4 changes: 1 addition & 3 deletions exercises/strings/strings2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn main() {
let word = String::from("green"); // Try not changing this line :)
if is_a_color_word(word) {
if is_a_color_word(&word) {
println!("That is a color word I know!");
} else {
println!("That is not a color word I know.");
Expand Down
8 changes: 3 additions & 5 deletions exercises/strings/strings3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn trim_me(input: &str) -> String {
// TODO: Remove whitespace from both ends of a string!
???
input.trim().to_string()
}

fn compose_me(input: &str) -> String {
// TODO: Add " world!" to the string! There's multiple ways to do this!
???
String::from(input) + " world!"
}

fn replace_me(input: &str) -> String {
// TODO: Replace "cars" in the string with "balloons"!
???
input.replace("car", "balloon")
}

#[cfg(test)]
Expand Down
21 changes: 10 additions & 11 deletions exercises/strings/strings4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//
// No hints this time!

// I AM NOT DONE

fn string_slice(arg: &str) {
println!("{}", arg);
Expand All @@ -17,14 +16,14 @@ fn string(arg: String) {
}

fn main() {
???("blue");
???("red".to_string());
???(String::from("hi"));
???("rust is fun!".to_owned());
???("nice weather".into());
???(format!("Interpolation {}", "Station"));
???(&String::from("abc")[0..1]);
???(" hello there ".trim());
???("Happy Monday!".to_string().replace("Mon", "Tues"));
???("mY sHiFt KeY iS sTiCkY".to_lowercase());
string_slice("blue");
string("red".to_string());
string(String::from("hi"));
string("rust is fun!".to_owned());
string("nice weather".into());
string(format!("Interpolation {}", "Station"));
string_slice(&String::from("abc")[0..1]);
string_slice(" hello there ".trim());
string("Happy Monday!".to_string().replace("Mon", "Tues"));
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
}
16 changes: 11 additions & 5 deletions exercises/structs/structs1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

struct ColorClassicStruct {
// TODO: Something goes here
red: u16,
green: u16,
blue: u16
}

struct ColorTupleStruct(/* TODO: Something goes here */);
struct ColorTupleStruct(u16, u16, u16/* TODO: Something goes here */);

#[derive(Debug)]
struct UnitLikeStruct;
Expand All @@ -23,7 +25,11 @@ mod tests {
#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
// let green =
let green = ColorClassicStruct {
red: 0,
green: 255,
blue: 0
};

assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
Expand All @@ -33,7 +39,7 @@ mod tests {
#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct!
// let green =
let green = ColorTupleStruct(0, 255, 0);

assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
Expand All @@ -43,7 +49,7 @@ mod tests {
#[test]
fn unit_structs() {
// TODO: Instantiate a unit-like struct!
// let unit_like_struct =
let unit_like_struct = UnitLikeStruct;
let message = format!("{:?}s are fun!", unit_like_struct);

assert_eq!(message, "UnitLikeStructs are fun!");
Expand Down
13 changes: 10 additions & 3 deletions exercises/structs/structs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(Debug)]
struct Order {
name: String,
Expand Down Expand Up @@ -38,7 +36,16 @@ mod tests {
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// let your_order =
let your_order = Order{
name: String::from("Hacker in Rust"),
year: order_template.year,
made_by_phone: order_template.made_by_phone,
made_by_mobile: order_template.made_by_mobile,
made_by_email: order_template.made_by_email,
item_number: order_template.item_number,
count: 1
};

assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
Expand Down
7 changes: 4 additions & 3 deletions exercises/structs/structs3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(Debug)]
struct Package {
Expand All @@ -29,12 +28,14 @@ impl Package {
}
}

fn is_international(&self) -> ??? {
fn is_international(&self) -> bool {
// Something goes here...
self.sender_country != self.recipient_country
}

fn get_fees(&self, cents_per_gram: i32) -> ??? {
fn get_fees(&self, cents_per_gram: i32) -> i32{
// Something goes here...
cents_per_gram * self.weight_in_grams
}
}

Expand Down

0 comments on commit 29dc22c

Please sign in to comment.