From cb7ed9e5b987ff1269f2d6ba54343936608b3240 Mon Sep 17 00:00:00 2001 From: Tyler Cardinal <83625450+tjcardinal@users.noreply.github.com> Date: Sat, 8 May 2021 13:35:31 -0500 Subject: [PATCH] Complete move semantics exercises (#5) --- exercises/move_semantics/move_semantics1.rs | 4 +--- exercises/move_semantics/move_semantics2.rs | 8 +++----- exercises/move_semantics/move_semantics3.rs | 4 +--- exercises/move_semantics/move_semantics4.rs | 8 ++------ 4 files changed, 7 insertions(+), 17 deletions(-) diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index e2f5876..a95f851 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -1,12 +1,10 @@ // move_semantics1.rs // Make me compile! Execute `rustlings hint move_semantics1` for hints :) -// I AM NOT DONE - fn main() { let vec0 = Vec::new(); - let vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs index bd21fbb..f967f85 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -2,12 +2,10 @@ // Make me compile without changing line 13! // Execute `rustlings hint move_semantics2` for hints :) -// I AM NOT DONE - fn main() { let vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(&vec0); // Do not change the following line! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); @@ -17,8 +15,8 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec<i32>) -> Vec<i32> { - let mut vec = vec; +fn fill_vec(vec: &Vec<i32>) -> Vec<i32> { + let mut vec = vec.to_vec(); vec.push(22); vec.push(44); diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index 43fef74..8be23e5 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -3,8 +3,6 @@ // (no lines with multiple semicolons necessary!) // Execute `rustlings hint move_semantics3` for hints :) -// I AM NOT DONE - fn main() { let vec0 = Vec::new(); @@ -17,7 +15,7 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec<i32>) -> Vec<i32> { +fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> { vec.push(22); vec.push(44); vec.push(66); diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 2a23c71..c3cf03d 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -4,12 +4,8 @@ // freshly created vector from fill_vec to its caller. // Execute `rustlings hint move_semantics4` for hints! -// I AM NOT DONE - fn main() { - let vec0 = Vec::new(); - - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); @@ -20,7 +16,7 @@ fn main() { // `fill_vec()` no longer takes `vec: Vec<i32>` as argument fn fill_vec() -> Vec<i32> { - let mut vec = vec; + let mut vec = Vec::new(); vec.push(22); vec.push(44);