I’m working on a Rust implementation of the Trapping Rain Water programming problem. I have a partial solution that looks idiomatic, although I am a beginner so any help in that respect is appreciated. The problem I am having is that I cannot take the reverse of a scan. Which I need in order to compute the right-to-left prefix scan max operation.

I tried to reason about the type system at first it doesn’t seem possible to take the reverse of a scan, since the scan operation produces results on-demand. OTOH since Vector is an ExactSizeIterator can’t we take advantage of this fact to reverse the scan for this specific trait?

Here’s my current implementation. It doesn’t compile because I try to reverse the scan.

```
use std::cmp;
fn scan_max(max: &mut u32, elem: u32) -> Option<u32> {
*max = cmp::max(*max, elem);
Some(*max)
}
pub fn capacity(heights: Vec<u32>) -> u32 {
let left_max = heights.iter().scan(0, |max, &e| scan_max(max, e));
let right_max = heights.iter().rev().scan(0, |max, &e| scan_max(max, e)).rev();
let min = left_max.zip(right_max).map(|(l, r)| cmp::min(l, r));
let delta = heights.iter().zip(min).map(|(h, m)| m - h);
delta.fold(0, |sum, x| sum + x)
}
#[test]
fn test_capacity() {
let heights = vec![2,6,3,5,2,8,1,4,2,2,5,3,5,7,4,1];
assert_eq!(capacity(heights), 35);
}
```