dimanche 27 novembre 2016

Using if inside of fold

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 foo(& self) -> f64 {
    -&self.current_domain.iter()
        .fold(0, |count, &(exists, _)| if exists {count + 1}) as f64
}

The compiler complained saying:

.fold(0, |count, &(exists, _)| if exists {count + 1}) as f64
                               ^^^^^^^^^^^^^^^^^^^^^ expected (), found integral variable

So I added a ; and got this:

.fold(0, |count, &(exists, _)| if exists {count + 1;}) as f64
                               ^^^^^^^^^^^^^^^^^^^^^^ expected integral variable, found ()

How do you correctly use an if statement inside of fold?

Aucun commentaire:

Enregistrer un commentaire