From 399ab328d8d407265c09563aa4ef4534b2503ff2 Mon Sep 17 00:00:00 2001 From: Sateesh Date: Mon, 17 May 2021 17:40:40 +0530 Subject: [PATCH] feat: Add move_semantics5 exercise. (#746) * feat: Add move_semantics5 exercise. * feat: Add option3 exercise * Address review comments. Fix typos, sentence formatting. * Remove unwanted newline. * Address review comments: make comment inline, fix format in print. --- exercises/move_semantics/move_semantics5.rs | 14 ++++++++++++++ exercises/option/option3.rs | 19 +++++++++++++++++++ info.toml | 21 +++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 exercises/move_semantics/move_semantics5.rs create mode 100644 exercises/option/option3.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/exercises/option/option3.rs b/exercises/option/option3.rs new file mode 100644 index 0000000..045d2ac --- /dev/null +++ b/exercises/option/option3.rs @@ -0,0 +1,19 @@ +// option3.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 = Some(Point { x: 100, y: 200 }); + + match y { + Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), + _ => println!("no match"), + } + y; // Fix without deleting this line. +} diff --git a/info.toml b/info.toml index 0535a77..0e9f428 100644 --- a/info.toml +++ b/info.toml @@ -210,6 +210,17 @@ 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]] @@ -578,6 +589,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"