use std::vec::Vec;
fn main () {
let percentages = vec![10.0, 11.0, 23.0, 34.0];
let balance = compound( 1000.00, percentages);
println!("Your balance is {}", balance );
}
fn compound(investment: f64, returns: Vec<f64> ) -> f64 {
returns.fold(investment, | value, perc | value * ( 1 + (perc / 100 )) )
}
Now, I am getting the error:
`error: no method named `fold` found for type `std::vec::Vec<f64>` in the current scope`
I am hacking up a trait tonight that implements this type of operation easily (passing integer or float and array -> f64) and share it on cargo, hopefully others won't struggle with edge cases.
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. )) )
}