Calculating average value of points around given point in a wider field 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:&amp;[i32]) -&gt; 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!(&quot;y: {:.?}, x: {:.?}&quot;,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!(&quot;{} / {} = {}&quot;,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:&amp;[i32]) -&gt; 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!(&quot;y: {:.?}, x: {:.?}&quot;,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::&lt;i32&gt;()
    }).sum();
    
    println!(&quot;{} / {} = {}&quot;,total,size,total / size);
    total / size
}

How could I improve this function?
Repl: https://repl.it/@JonathanWoollet/DraftyTautAdware
We don't need an x_range. We need a range of proper indices over space, as it enables us to use space[start..stop].iter().sum(). We need a combination of x_range and y_range.
So let's start by building one from hand:
fn local_average((x, y): (i32, i32), n: i32, (width, height): (i32, i32), space: &amp;[i32]) -&gt; 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);

    let size = (x_range.end - x_range.start) * (y_range.end - y_range.start);
    let mut total: i32 = 0;

    for yi in y_range {
        let start = (yi * width + x_range.start) as usize;
        let end = start + ((x_range.end - x_range.start) as usize);

        total += space[start..end].iter().sum::&lt;i32&gt;();
    }

    total / size
}

Note that the code above was formatted by rustfmt. The rustfmt-style is very common in Rust projects, and several projects reject contributions until they are properly formatted. For cargo based projects use cargo fmt.
Either way, we now replaced the inner loop by a single sum(), which is more explicit about our goal. This is also a mixture of both your code styles. At this point, we might as well replace x_range by x_size, but that's a question of style.
For an easier use of local_average, we could introduce type Point = (i32, i32) and type Rect = (i32, i32), however, again, that's a matter of preference.