From 51e237d5f97610294798710ef8ba5349c2fd50c7 Mon Sep 17 00:00:00 2001 From: liv Date: Mon, 4 Sep 2023 14:20:37 +0200 Subject: [PATCH] fix: refactor move semantics 1-4 into tests --- exercises/move_semantics/move_semantics1.rs | 15 +++++---------- exercises/move_semantics/move_semantics2.rs | 18 ++++++------------ exercises/move_semantics/move_semantics3.rs | 13 ++++--------- exercises/move_semantics/move_semantics4.rs | 16 ++++++---------- info.toml | 16 ++++++---------- 5 files changed, 27 insertions(+), 51 deletions(-) diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index 710d20d..e063937 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -5,24 +5,19 @@ // I AM NOT DONE +#[test] fn main() { - let vec0 = Vec::new(); + let vec0 = vec![22, 44, 66]; let vec1 = fill_vec(vec0); - println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); - - vec1.push(88); - - println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); + assert_eq!(vec1, vec![22, 44, 66, 88]); } fn fill_vec(vec: Vec) -> Vec { - let mut vec = vec; + let vec = vec; - vec.push(22); - vec.push(44); - vec.push(66); + vec.push(88); vec } diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs index 72d37fa..baf6bcc 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -1,32 +1,26 @@ // move_semantics2.rs // -// Expected output: -// vec0 has length 3, with contents `[22, 44, 66]` -// vec1 has length 4, with contents `[22, 44, 66, 88]` +// Make the test pass by finding a way to keep both Vecs separate! // // Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand // for a hint. // I AM NOT DONE +#[test] fn main() { - let vec0 = Vec::new(); + let vec0 = vec![22, 44, 66]; let mut vec1 = fill_vec(vec0); - println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0); - - vec1.push(88); - - println!("{} has length {}, with contents `{:?}`", "vec1", vec1.len(), vec1); + assert_eq!(vec0, vec![22, 44, 66]); + assert_eq!(vec1, vec![22, 44, 66, 88]); } fn fill_vec(vec: Vec) -> Vec { let mut vec = vec; - vec.push(22); - vec.push(44); - vec.push(66); + vec.push(88); vec } diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index ea21493..69e564a 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -8,22 +8,17 @@ // I AM NOT DONE +#[test] fn main() { - let vec0 = Vec::new(); + let vec0 = vec![22, 44, 66]; let mut vec1 = fill_vec(vec0); - println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); - - vec1.push(88); - - println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); + assert_eq!(vec1, vec![22, 44, 66, 88]); } fn fill_vec(vec: Vec) -> Vec { - vec.push(22); - vec.push(44); - vec.push(66); + vec.push(88); vec } diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 75a3b6b..80b49db 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -9,25 +9,21 @@ // I AM NOT DONE +#[test] fn main() { - let vec0 = Vec::new(); + let vec0 = vec![22, 44, 66]; let mut vec1 = fill_vec(vec0); - println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); - - vec1.push(88); - - println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); + assert_eq!(vec1, vec![22, 44, 66, 88]); } -// `fill_vec()` no longer takes `vec: Vec` as argument +// `fill_vec()` no longer takes `vec: Vec` as argument - don't change this! fn fill_vec() -> Vec { + // Instead, let's create and fill the Vec in here - how do you do that? let mut vec = vec; - vec.push(22); - vec.push(44); - vec.push(66); + vec.push(88); vec } diff --git a/info.toml b/info.toml index 0aca022..b41985e 100644 --- a/info.toml +++ b/info.toml @@ -284,9 +284,9 @@ better. What do you think is the more commonly used pattern under Rust developer [[exercises]] name = "move_semantics1" path = "exercises/move_semantics/move_semantics1.rs" -mode = "compile" +mode = "test" hint = """ -So you've got the "cannot borrow immutable local variable `vec1` as mutable" error on the line +So you've got the "cannot borrow immutable local variable `vec` as mutable" error on the line where we push an element to the vector, right? The fix for this is going to be adding one keyword, and the addition is NOT on the line where we push to the vector (where the error is). @@ -296,7 +296,7 @@ Also: Try accessing `vec0` after having called `fill_vec()`. See what happens!"" [[exercises]] name = "move_semantics2" path = "exercises/move_semantics/move_semantics2.rs" -mode = "compile" +mode = "test" hint = """ When running this exercise for the first time, you'll notice an error about "borrow of moved value". In Rust, when an argument is passed to a function and @@ -309,16 +309,12 @@ Rust provides a couple of different ways to mitigate this issue, feel free to tr 2. Make `fill_vec` borrow its argument instead of taking ownership of it, and then copy the data within the function (`vec.clone()`) in order to return an owned `Vec`. -3. Or, you could make `fill_vec` *mutably* borrow a reference to its argument (which will need to be - mutable), modify it directly, then not return anything. This means that `vec0` will change over the - course of the function, and makes `vec1` redundant (make sure to change the parameters of the `println!` - statements if you go this route) """ [[exercises]] name = "move_semantics3" path = "exercises/move_semantics/move_semantics3.rs" -mode = "compile" +mode = "test" hint = """ The difference between this one and the previous ones is that the first line of `fn fill_vec` that had `let mut vec = vec;` is no longer there. You can, @@ -328,7 +324,7 @@ an existing binding to be a mutable binding instead of an immutable one :)""" [[exercises]] name = "move_semantics4" path = "exercises/move_semantics/move_semantics4.rs" -mode = "compile" +mode = "test" hint = """ Stop reading whenever you feel like you have enough direction :) Or try doing one step and then fixing the compiler errors that result! @@ -337,7 +333,7 @@ So the end goal is to: - so then `vec0` doesn't exist, so we can't pass it to `fill_vec` - `fill_vec` has had its signature changed, which our call should reflect - 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`""" + a new vec in `fill_vec`, and fill it with the expected values""" [[exercises]] name = "move_semantics5"