From 51775de8dc0451d7d5daf0868eba9439fe98b8aa Mon Sep 17 00:00:00 2001 From: Rock070 Date: Sun, 17 Dec 2023 15:42:04 +0800 Subject: [PATCH] Fix multiplication logic in vec_loop and vec_map functions --- exercises/05_vecs/vecs2.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/exercises/05_vecs/vecs2.rs b/exercises/05_vecs/vecs2.rs index e92c970a..1ae46fc9 100644 --- a/exercises/05_vecs/vecs2.rs +++ b/exercises/05_vecs/vecs2.rs @@ -6,14 +6,13 @@ // Make me pass the test! // // Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. - -// I AM NOT DONE +// [[NOTE]] 解引用, map fn vec_loop(mut v: Vec) -> Vec { + for element in v.iter_mut() { - // TODO: Fill this up so that each element in the Vec `v` is // multiplied by 2. - ??? + *element *= 2; // Fill this up so that each element in the Vec `v` is } // At this point, `v` should be equal to [4, 8, 12, 16, 20]. @@ -22,10 +21,9 @@ fn vec_loop(mut v: Vec) -> Vec { fn vec_map(v: &Vec) -> Vec { v.iter().map(|element| { - // TODO: Do the same thing as above - but instead of mutating the // Vec, you can just return the new number! - ??? - }).collect() + element * 2 // Do the same thing as above - but instead of mutating the + }).collect::>() } #[cfg(test)]