I need to count the length of a vector of (bool, i32)
where if the bool
is true I increment count. I'm using fold to do this:
fn main() {
let domain = [(true, 1), (false, 2), (true, 3)];
let dom_count = domain.iter()
.fold(0, |count, &(exists, _)| if exists {count + 1});
println!("dom_count: {}", dom_count);
}
The compiler complained saying:
.fold(0, |count, &(exists, _)| if exists {count + 1})
^^^^^^^^^^^^^^^^^^^^^ expected (), found integral variable
So I added a ;
and got this:
.fold(0, |count, &(exists, _)| if exists {count + 1;})
^^^^^^^^^^^^^^^^^^^^^^ expected integral variable, found ()
How do you correctly use an if
statement inside of fold
?