aboutsummaryrefslogtreecommitdiff
path: root/challenge-033/rage311/rust/ch-2.rs
blob: 72aed2ad4cc19588a142e80941203cea20d82753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
fn main() {
    let mut results: Vec<Vec<Option<u8>>> = vec![(0..12)
        .map(|n| match n {
            0 => None,
            _ => Some(n),
        })
        .collect()];

    for i in 1..12 {
        let mut row: Vec<Option<u8>> = vec![Some(i)];
        for j in 1..12 {
            row.push(if j >= i { Some(i * j) } else { None });
        }
        results.push(row);
    }

    results.iter().for_each(|row| {
        println!(
            "{}",
            row.iter()
                .map(|product| format!(
                    "{:>3}",
                    match product {
                        Some(p) => p.to_string(),
                        None => "".to_string(),
                    }
                ))
                .collect::<Vec<_>>()
                .join(" ")
        )
    });
}

/* Output:
      1   2   3   4   5   6   7   8   9  10  11
  1   1   2   3   4   5   6   7   8   9  10  11
  2       4   6   8  10  12  14  16  18  20  22
  3           9  12  15  18  21  24  27  30  33
  4              16  20  24  28  32  36  40  44
  5                  25  30  35  40  45  50  55
  6                      36  42  48  54  60  66
  7                          49  56  63  70  77
  8                              64  72  80  88
  9                                  81  90  99
 10                                     100 110
 11                                         121
*/