It's idiomatically done with newline separators, e.g:
let sum = vec_of_ints
.iter()
.filter(|x| x > 5)
.map(|x| x.pow(2))
.take(3)
.sum()
That is, iterate my vec, only consider values > 5, square them, take the first 3, them sum them.Consider the imperative code:
let mut sum = 0;
let mut n = 0;
for x in vec_of_ints {
if x > 5 {
sum += x.pow(2);
n += 1;
if n == 3 {
break;
}
}
}
I think the chained combinator approach is much easier to see exactly what's happening (and is often higher performance!)