Fix multiplication logic in vec_loop and vec_map functions

This commit is contained in:
Rock070 2023-12-17 15:42:04 +08:00
parent 294c97b9fc
commit 51775de8dc

View File

@ -6,14 +6,13 @@
// Make me pass the test! // Make me pass the test!
// //
// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint.
// [[NOTE]] 解引用, map
// I AM NOT DONE
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> { fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for element in v.iter_mut() { for element in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2. // 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]. // At this point, `v` should be equal to [4, 8, 12, 16, 20].
@ -22,10 +21,9 @@ fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
fn vec_map(v: &Vec<i32>) -> Vec<i32> { fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|element| { v.iter().map(|element| {
// TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number! // Vec, you can just return the new number!
??? element * 2 // Do the same thing as above - but instead of mutating the
}).collect() }).collect::<Vec<i32>>()
} }
#[cfg(test)] #[cfg(test)]