From e3a20b8bc899eae9dfb440b885836812fa8614ee Mon Sep 17 00:00:00 2001 From: Nidhal Messaoudi Date: Mon, 27 Feb 2023 21:16:40 +0100 Subject: [PATCH] Learning about vectors --- exercises/move_semantics/move_semantics1.rs | 4 +--- exercises/move_semantics/move_semantics2.rs | 10 ++++------ exercises/move_semantics/move_semantics3.rs | 4 +--- exercises/vecs/vecs1.rs | 4 +--- exercises/vecs/vecs2.rs | 6 ++---- 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index aac6dfc3..c60ee74f 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -1,12 +1,10 @@ // move_semantics1.rs // Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint. -// 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 64870850..229f13bd 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 or moving line 10! // Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { - let vec0 = Vec::new(); + let mut vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(&mut vec0); // Do not change the following line! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); @@ -17,12 +15,12 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { +fn fill_vec(vec: &mut Vec) -> Vec { let mut vec = vec; vec.push(22); vec.push(44); vec.push(66); - vec + vec.to_vec() } diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index eaa30e33..bb830234 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` or use the `hint` watch subcommand for a hint. -// 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) -> Vec { +fn fill_vec(mut vec: Vec) -> Vec { vec.push(22); vec.push(44); vec.push(66); diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs index 4e8c4cbb..0e8e2f73 100644 --- a/exercises/vecs/vecs1.rs +++ b/exercises/vecs/vecs1.rs @@ -4,11 +4,9 @@ // Make me compile and pass the test! // Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn array_and_vec() -> ([i32; 4], Vec) { let a = [10, 20, 30, 40]; // a plain array - let v = // TODO: declare your vector here with the macro for vectors + let v = vec![10, 20, 30, 40]; // TODO: declare your vector here with the macro for vectors (a, v) } diff --git a/exercises/vecs/vecs2.rs b/exercises/vecs/vecs2.rs index 5bea09a2..d016daaf 100644 --- a/exercises/vecs/vecs2.rs +++ b/exercises/vecs/vecs2.rs @@ -6,13 +6,11 @@ // // Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn vec_loop(mut v: Vec) -> Vec { for i in v.iter_mut() { // TODO: Fill this up so that each element in the Vec `v` is // multiplied by 2. - ??? + *i *= 2; } // At this point, `v` should be equal to [4, 8, 12, 16, 20]. @@ -23,7 +21,7 @@ fn vec_map(v: &Vec) -> Vec { v.iter().map(|num| { // TODO: Do the same thing as above - but instead of mutating the // Vec, you can just return the new number! - ??? + num * 2 }).collect() }