From 0d1db97a937393bcf92811bb5898e73472658593 Mon Sep 17 00:00:00 2001 From: Ruud Salym J Erie Date: Sat, 26 Nov 2022 22:24:49 -0300 Subject: [PATCH] adding completed excercises for move semantics and vecs --- exercises/move_semantics/move_semantics1.rs | 3 +-- exercises/move_semantics/move_semantics2.rs | 3 +-- exercises/move_semantics/move_semantics3.rs | 3 +-- exercises/move_semantics/move_semantics4.rs | 8 ++------ exercises/move_semantics/move_semantics5.rs | 5 ++--- exercises/move_semantics/move_semantics6.rs | 10 ++++------ exercises/vecs/vecs2.rs | 11 +++++++---- 7 files changed, 18 insertions(+), 25 deletions(-) diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index aac6dfc3..edda9ac3 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -1,12 +1,11 @@ // 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..6fe0e8a1 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -2,12 +2,11 @@ // 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 vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0.clone()); // Do not change the following line! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index eaa30e33..0276e1ba 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -3,7 +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 +16,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/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 99834ec3..d58c4b96 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -4,12 +4,9 @@ // function. // Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand for a hint. -// 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); @@ -18,9 +15,8 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -// `fill_vec()` no longer takes `vec: Vec` as argument fn fill_vec() -> Vec { - let mut vec = vec; + let mut vec = Vec::new(); vec.push(22); vec.push(44); diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs index 36eae127..66d1f2e2 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -3,13 +3,12 @@ // adding, changing or removing any of them. // Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let mut x = 100; let y = &mut x; - let z = &mut x; *y += 100; + let z = &mut x; *z += 1000; + assert_eq!(x, 1200); } diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs index eb52a848..2b43cfc4 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -2,14 +2,12 @@ // Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand for a hint. // You can't change anything except adding or removing references. -// I AM NOT DONE - fn main() { let data = "Rust is great!".to_string(); - get_char(data); + get_char(data.clone()); - string_uppercase(&data); + string_uppercase(data); } // Should not take ownership @@ -18,8 +16,8 @@ fn get_char(data: String) -> char { } // Should take ownership -fn string_uppercase(mut data: &String) { - data = &data.to_uppercase(); +fn string_uppercase(mut data: String) { + data = data.to_uppercase(); println!("{}", data); } diff --git a/exercises/vecs/vecs2.rs b/exercises/vecs/vecs2.rs index 4dbb190d..16804d55 100644 --- a/exercises/vecs/vecs2.rs +++ b/exercises/vecs/vecs2.rs @@ -6,7 +6,6 @@ // // 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() { @@ -18,11 +17,15 @@ fn vec_loop(mut v: Vec) -> Vec { } fn vec_map(v: &Vec) -> Vec { - v.iter().map(|num| { + 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() + num * 2 + + }) + .collect() + } #[cfg(test)]