Solution Review: Resizing a Vector
This lesson will give a detailed review of the solution to the challenge in the previous lesson.
We'll cover the following...
Solution
Press + to interact
fn test(my_vec: &mut Vec<u32>)-> &mut Vec<u32>{let middle = (my_vec.len())/2;my_vec.pop();my_vec.remove(middle - 1);let mut sum : u32 = 0;for v in my_vec.iter(){sum = sum + v;}my_vec.push(sum);my_vec}fn main(){let mut v1 = vec![1, 5, 7, 9];println!("Original Vector: {:?}", v1);println!("Updated Vector: {:?}", test(&mut v1));let mut v2 = vec![1, 2, 3, 1, 2, 6];println!("Original Vector: {:?}", v2);println!("Updated Vector: {:?}", test(&mut v2));}
Explanation
A function test
is declared with ...