Now that the question of how to pass in Vecs has been answered, another point should be brought up:
If your argument type is a Vec, the ownership of the Vec is passed to the called function and so the Vec is no longer available to the caller. So, if you’d try something like:
let balance = compound( 1000.00, percentages);
let balance = compound( balance, percentages);
it will fail to compile with the error "use of moved value: percentages
".
For this reason, a function like compound would usually take the returns Array/Vec as a reference. (i.e. &Vec, with the caller passing it using &percentages).
Now, once you take an immutable reference to a Vec, you can almost always rather take a slice (i.e. &[f64]), so that the caller can pass in data from a Vec OR an array:
fn main () {
let percentages = [10., 11., 23., 34.];
let balance = compound( 1000.00, &percentages);
println!("Your balance is {}", balance );
}
fn compound(investment: f64, returns: &[f64] ) -> f64 {
returns.iter().fold(investment, | value, &perc | value * ( 1. + (perc / 100. )) )
}
Playground