2020-10-26 13:54:32 +07:00
|
|
|
// vec2.rs
|
2021-04-25 16:29:39 +07:00
|
|
|
//
|
2020-10-26 13:54:32 +07:00
|
|
|
// A Vec of even numbers is given. Your task is to complete the loop
|
|
|
|
// so that each number in the Vec is multiplied by 2.
|
|
|
|
//
|
|
|
|
// Make me pass the test!
|
|
|
|
//
|
2021-04-25 16:29:39 +07:00
|
|
|
// If you need help, open the corresponding README.md or run: rustlings hint vec2
|
2020-10-26 13:54:32 +07:00
|
|
|
|
|
|
|
// I AM NOT DONE
|
|
|
|
|
|
|
|
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
|
|
|
for i in v.iter_mut() {
|
|
|
|
// TODO: Fill this up so that each element in the Vec `v` is
|
|
|
|
// multiplied by 2.
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
|
|
|
v
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vec_loop() {
|
|
|
|
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
|
|
|
|
let ans = vec_loop(v.clone());
|
|
|
|
|
2021-03-19 00:39:22 +07:00
|
|
|
assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
|
2020-10-26 13:54:32 +07:00
|
|
|
}
|
|
|
|
}
|