exercism/
minesweeper.rs

1static NEIGHBOURHOOD_OFFSETS: &[(i32, i32)] = &[
2    (-1, -1), (0, -1), (1, -1),
3    (-1,  0),          (1,  0),
4    (-1,  1), (0,  1), (1,  1),
5];
6
7pub fn annotate(minefield: &[&str]) -> Vec<String> {
8    minefield.iter().enumerate().map(|(y, row)| {
9        row.chars().enumerate().map(|(x, c)| match c {
10            '*' => '*',
11            _ => {
12                let count = NEIGHBOURHOOD_OFFSETS.iter()
13                    .filter(|&&(ox, oy)| {
14                        let nx = x as i32 + ox;
15                        let ny = y as i32 + oy;
16                        nx >= 0 && nx < row.len() as i32 && ny >= 0 && ny < minefield.len() as i32
17                    })
18                    .filter(|&&(ox, oy)| {
19                        let nx = x as i32 + ox;
20                        let ny = y as i32 + oy;
21                        minefield[ny as usize].chars().nth(nx as usize).unwrap() == '*'
22                    })
23                    .count();
24
25                if count > 0 {
26                    (count as u8 + b'0') as char
27                } else {
28                    ' '
29                }
30            }
31        }).collect()
32    }).collect()
33}