Trapping Rain Water in Rust

impl Solution {
    fn trap(height: Vec<i32>) -> i32 {
        let n = height.len();
        if n == 0 {
            return 0;
        }

        let mut left_max = vec![0; n];
        let mut right_max = vec![0; n];
        let mut water = 0;

        // Fill the left_max array
        left_max[0] = height[0];
        for i in 1..n {
            left_max[i] = std::cmp::max(left_max[i - 1], height[i]);
        }

        // Fill the right_max array
        right_max[n - 1] = height[n - 1];
        for i in (0..n - 1).rev() {
            right_max[i] = std::cmp::max(right_max[i + 1], height[i]);
        }

        // Calculate trapped rainwater
        for i in 0..n {
            water += std::cmp::min(left_max[i], right_max[i]) - height[i];
        }

        water
    }
}

Next