I'm trying to practice using Rust's functional capabilities.
For example, I would like to convert this loop:
levels: Vec<Vec<u8>> = //< Built by something
let mut total = 0;
for (x, y) in iproduct!(0..10, 0..10) {
if levels[x][y] > 9 {
total += count_something(levels, x, y);
}
}
// Edit: here's the `count_something` function signature
fn count_something (levels: &mut Vec<Vec<u8>>, x: usize, y: usize) -> usize {
// Count
}
This is the result of my functional refactoring:
iproduct!(0..10, 0..10)
.filter(|(x, y)| levels[*x][*y] > 9)
.map(|(x, y)| count_something(levels, x, y))
.sum()
The problem is: this code won't compile.
The error: error[E0500]: closure requires unique access to *levels but it is already borrowed.
I don't see why filter borrows the levels 2D matrix.
My mental model of what is happening under the hood seems inadequate.