I'm writing a function that, given a Vec<i32>
of numbers, lists the combinations that add up to 0. The code works, but it feels amateurish.
Specifically, the inside of the loop looks really ugly -- there must be a simpler way of doing this that avoids unwrap()
and the repetition. Maybe with match
?
fn calc_zer(input: Vec<i32>) -> Vec<(i32, i32, i32)> {
let mut output: Vec<(i32, i32, i32)> = Vec::new();
for a in (0..input.len() - 2) {
for b in (a+1..input.len() -1) {
for c in (b+1..input.len()) {
if (input.get(a).unwrap() + input.get(b).unwrap() + input.get(c).unwrap() == 0) {
output.push((*input.get(a).unwrap(), *input.get(b).unwrap(), *input.get(c).unwrap()));
}
}
}
}
output
}