I'm new to Rust, and I'm looking for a good way to calculate the sum of products in Rust.
I mean if there is a Vector like [3, 4, 5], I wanna calculate (3 * 4) + (3 * 5) + (4 * 5).
this is my method,
fn main() {
let a = [3, 4, 5];
assert_eq!(
47,
[3, 4, 5].iter().enumerate().fold(0, |mut s, (i, v)| {
for ii in i + 1..a.len() {
s += *v * a[ii];
}
s
})
);
}
If you know the better one, I'd love to know!