From 528e45ea4c9e4f7fbdfd12a8832e4843d16ffdb6 Mon Sep 17 00:00:00 2001 From: Sateesh Basavaraju <sateeshkumarb@yahoo.com> Date: Thu, 6 May 2021 23:17:27 +0530 Subject: [PATCH 1/6] feat: Add move_semantics5 exercise. --- exercises/move_semantics/move_semantics5.rs | 14 ++++++++++++++ info.toml | 7 +++++++ 2 files changed, 21 insertions(+) create mode 100644 exercises/move_semantics/move_semantics5.rs diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs new file mode 100644 index 0000000..4f8128c --- /dev/null +++ b/exercises/move_semantics/move_semantics5.rs @@ -0,0 +1,14 @@ +// move_semantics5.rs +// Make me compile without adding any newlines or removing any of the lines. +// Execute `rustlings hint move_semantics5` for hints :) + +// I AM NOT DONE + +fn main() { + let mut x = 100; + let y = &mut x; + let z = &mut *y; + *y += 100; + *z += 1000; + assert_eq!(x, 1200); +} diff --git a/info.toml b/info.toml index 82e1195..9f35d50 100644 --- a/info.toml +++ b/info.toml @@ -210,6 +210,13 @@ So the end goal is to: - since we're not creating a new vec in `main` anymore, we need to create a new vec in `fill_vec`, similarly to the way we did in `main`""" +[[exercises]] +name = "move_semantics5" +path = "exercises/move_semantics/move_semantics5.rs" +mode = "compile" +hint = """Carefully reason about the range in which each mutable reference is in vogue. Does updating the value of referrent (x) immediately after the mutable reference is taken helps ? Read more about 'Mutable Referenes' in the book's section References and Borrowing': https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.""" + + # PRIMITIVE TYPES [[exercises]] From 9b3e700edf37fe2e5caaa0cd818174807dfe838b Mon Sep 17 00:00:00 2001 From: Sateesh Basavaraju <sateeshkumarb@yahoo.com> Date: Sat, 8 May 2021 15:22:27 +0530 Subject: [PATCH 2/6] feat: Add option3 exercise --- exercises/option/option3.rs | 20 ++++++++++++++++++++ info.toml | 10 ++++++++++ 2 files changed, 30 insertions(+) create mode 100644 exercises/option/option3.rs diff --git a/exercises/option/option3.rs b/exercises/option/option3.rs new file mode 100644 index 0000000..77dd4f0 --- /dev/null +++ b/exercises/option/option3.rs @@ -0,0 +1,20 @@ +// option2.rs +// Make me compile! Execute `rustlings hint option3` for hints + +// I AM NOT DONE + +struct Point { + x: i32, + y: i32, +} + +fn main() { + let y: Option<Point> = Some(Point { x: 100, y: 200 }); + + match y { + Some(p) => println!("Co-ordinates are{},{} ", p.x, p.y), + _ => println!("no match"), + } + // Fix without deleting this line. + y; +} diff --git a/info.toml b/info.toml index 9f35d50..7171d86 100644 --- a/info.toml +++ b/info.toml @@ -585,6 +585,16 @@ For example: Some(Some(variable)) = variable2 Also see Option::flatten """ +[[exercises]] +name = "option3" +path = "exercises/option/option3.rs" +mode = "compile" +hint = """ +The compiler says a partial move happened in the `match` +statement. How can this be avoided ? The compiler shows the correction +needed. After making the correction as suggested by the compiler, do +read: https://doc.rust-lang.org/std/keyword.ref.html""" + [[exercises]] name = "result1" path = "exercises/error_handling/result1.rs" From f3663eeb93101ad1974b6df1dfc51a5708c819c6 Mon Sep 17 00:00:00 2001 From: Sateesh Basavaraju <sateeshkumarb@yahoo.com> Date: Sat, 8 May 2021 16:15:10 +0530 Subject: [PATCH 3/6] Address review comments. Fix typos, sentence formatting. --- exercises/option/option3.rs | 2 +- info.toml | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/exercises/option/option3.rs b/exercises/option/option3.rs index 77dd4f0..e45fb8f 100644 --- a/exercises/option/option3.rs +++ b/exercises/option/option3.rs @@ -1,4 +1,4 @@ -// option2.rs +// option3.rs // Make me compile! Execute `rustlings hint option3` for hints // I AM NOT DONE diff --git a/info.toml b/info.toml index 7171d86..6567be7 100644 --- a/info.toml +++ b/info.toml @@ -214,7 +214,12 @@ So the end goal is to: name = "move_semantics5" path = "exercises/move_semantics/move_semantics5.rs" mode = "compile" -hint = """Carefully reason about the range in which each mutable reference is in vogue. Does updating the value of referrent (x) immediately after the mutable reference is taken helps ? Read more about 'Mutable Referenes' in the book's section References and Borrowing': https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.""" +hint = """ +Carefully reason about the range in which each mutable reference is in +vogue. Does updating the value of referrent (x) immediately after the +mutable reference is taken helps? Read more about 'Mutable Referenes' +in the book's section References and Borrowing': +https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.""" # PRIMITIVE TYPES @@ -591,7 +596,7 @@ path = "exercises/option/option3.rs" mode = "compile" hint = """ The compiler says a partial move happened in the `match` -statement. How can this be avoided ? The compiler shows the correction +statement. How can this be avoided? The compiler shows the correction needed. After making the correction as suggested by the compiler, do read: https://doc.rust-lang.org/std/keyword.ref.html""" From 8587f2c53ec7c16f9e77ec48f6ce97d462c415f3 Mon Sep 17 00:00:00 2001 From: Sateesh Basavaraju <sateeshkumarb@yahoo.com> Date: Sat, 8 May 2021 16:17:58 +0530 Subject: [PATCH 4/6] Remove unwanted newline. --- info.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/info.toml b/info.toml index 6567be7..4fe1279 100644 --- a/info.toml +++ b/info.toml @@ -221,7 +221,6 @@ mutable reference is taken helps? Read more about 'Mutable Referenes' in the book's section References and Borrowing': https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.""" - # PRIMITIVE TYPES [[exercises]] From 1f2380a312f50f27f7d3a4590c3fe30d97310212 Mon Sep 17 00:00:00 2001 From: Sateesh Basavaraju <sateeshkumarb@yahoo.com> Date: Thu, 13 May 2021 21:54:50 +0530 Subject: [PATCH 5/6] Address review comments: make comment inline, fix format in print. --- exercises/option/option3.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/exercises/option/option3.rs b/exercises/option/option3.rs index e45fb8f..045d2ac 100644 --- a/exercises/option/option3.rs +++ b/exercises/option/option3.rs @@ -12,9 +12,8 @@ fn main() { let y: Option<Point> = Some(Point { x: 100, y: 200 }); match y { - Some(p) => println!("Co-ordinates are{},{} ", p.x, p.y), + Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => println!("no match"), } - // Fix without deleting this line. - y; + y; // Fix without deleting this line. } From f25c3fbf48fc77cb0a7bbc26642ac59e3ae5fdda Mon Sep 17 00:00:00 2001 From: Sateesh Basavaraju <sateeshkumarb@yahoo.com> Date: Fri, 4 Jun 2021 16:09:12 +0530 Subject: [PATCH 6/6] feat: Add hashmap3 exercise. --- exercises/collections/hashmap3.rs | 88 +++++++++++++++++++++++++++++++ info.toml | 11 ++++ 2 files changed, 99 insertions(+) create mode 100644 exercises/collections/hashmap3.rs diff --git a/exercises/collections/hashmap3.rs b/exercises/collections/hashmap3.rs new file mode 100644 index 0000000..60f6016 --- /dev/null +++ b/exercises/collections/hashmap3.rs @@ -0,0 +1,88 @@ +// hashmap3.rs + +// A list of scores (one per line) of a match is given. Each line is +// of the form : +// <team_1_name>,<team_2_name>,<team_1_goals>,<team_2_goals> +// Example: England,France,4,2 (England scored 4 goals, France 2). + +// You have to build a scores table containing name of the team, goals +// the team scored, and goals the team conceded. One approach to build +// the scores table is to use a Hashmap. The solution is partially +// written to use a Hashmap, complete it to pass the test. + +// Make me pass the tests! + +// Execute the command `rustlings hint hashmap3` if you need +// hints. + +// I AM NOT DONE + +use std::collections::HashMap; + +// A structure to store team name and its goal details. +struct Team { + name: String, + goals_scored: u8, + goals_conceded: u8, +} + +fn build_scores_table(results: String) -> HashMap<String, Team> { + // name of the team is the key and corresponding team is the value. + let mut scores: HashMap<String, Team> = HashMap::new(); + + for r in results.lines() { + let v: Vec<&str> = r.split(',').collect(); + let team_1_name = v[0].to_string(); + 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(); + // TODO: Populate scores table with details extracted from the + // current line. Keep in mind that goals scored by team_1, + // will be number of goals conceded by the team_2, similarly + // goals scored by team_2 will be number of goals conceded by + // team_1. + } + scores +} + +#[cfg(test)] +mod tests { + use super::*; + + fn get_results() -> String { + let results = "".to_string() + + "England,France,4,2\n" + + "France,Italy,3,1\n" + + "Poland,Spain,2,0\n" + + "Germany,England,2,1\n"; + results + } + + #[test] + fn build_scores() { + let scores = build_scores_table(get_results()); + + let mut keys: Vec<&String> = scores.keys().collect(); + keys.sort(); + assert_eq!( + keys, + vec!["England", "France", "Germany", "Italy", "Poland", "Spain"] + ); + } + + #[test] + fn validate_team_score_1() { + let scores = build_scores_table(get_results()); + let team = scores.get("England").unwrap(); + assert_eq!(team.goals_scored, 5); + assert_eq!(team.goals_conceded, 4); + } + + #[test] + fn validate_team_score_2() { + let scores = build_scores_table(get_results()); + let team = scores.get("Spain").unwrap(); + assert_eq!(team.goals_scored, 0); + assert_eq!(team.goals_conceded, 2); + } +} diff --git a/info.toml b/info.toml index 8c8f7c1..8a912e4 100644 --- a/info.toml +++ b/info.toml @@ -417,6 +417,17 @@ Use the `entry()` and `or_insert()` methods of `HashMap` to achieve this. Learn more at https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value """ +[[exercises]] +name = "hashmap3" +path = "exercises/collections/hashmap3.rs" +mode = "test" +hint = """ +Hint 1: Use the `entry()` and `or_insert()` methods of `HashMap` to insert entries corresponding to each team in the scores table. +Learn more at https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value +Hint 2: If there is already an entry for a given key, the value returned by `entry()` can be updated based on the existing value. +Learn more at https://doc.rust-lang.org/book/ch08-03-hash-maps.html#updating-a-value-based-on-the-old-value +""" + # STRINGS [[exercises]]