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!
//
// 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<i32>) -> Vec<i32> {
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<i32>) -> Vec<i32> {
fn vec_map(v: &Vec<i32>) -> Vec<i32> {
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::<Vec<i32>>()
}
#[cfg(test)]