I need to calculate the average value of points around a given point in a wider field.

For this I have a function:

```
fn local_average((x,y):(i32,i32),n:i32,(width,height):(i32,i32),space:&(i32)) -> i32 {
let y_range = cmp::max(y-n,0)..cmp::min(y+n+1,height);
let x_range = cmp::max(x-n,0)..cmp::min(x+n+1,width);
println!("y: {:.?}, x: {:.?}",y_range,x_range);
let size = (x_range.end-x_range.start) * (y_range.end-y_range.start);
let mut total: i32 = 0;
for yi in y_range {
for xi in x_range.clone() {
let i = yi * width + xi;
total += space(i as usize);
}
}
println!("{} / {} = {}",total,size,total / size);
total / size
}
```

The thing that bothers me here is `x_range.clone()`

, I feel like this is not a good approach.

I could also implement this in a more function manor but I’m unsure if this is better as I find it to be less readable:

```
fn local_average_functional((x,y):(i32,i32),n:i32,(width,height):(i32,i32),space:&(i32)) -> i32 {
let y_range = cmp::max(y-n,0)..cmp::min(y+n+1,height);
let x_range = cmp::max(x-n,0)..cmp::min(x+n+1,width);
println!("y: {:.?}, x: {:.?}",y_range,x_range);
let size = (x_range.end-x_range.start) * (y_range.end-y_range.start);
let total: i32 = y_range.map(|yl| {
x_range.clone().map(|xl| {
let il = yl * width as i32 + xl;
space(il as usize) as i32
}).sum::<i32>()
}).sum();
println!("{} / {} = {}",total,size,total / size);
total / size
}
```

How could I improve this function?

Repl: https://repl.it/@JonathanWoollet/DraftyTautAdware