Skip to content

Commit 198d36d

Browse files
authored
dominoes: make test code more idiomatic (#2127)
I've had a mentee copy the "normalize" function from the test code. I didn't notice at first and explained it's unidiomatic to use a match statement only for the guards, because it's just a weird way of writing an if-else-statement. This version isn't an if-else-statement (which would've been fine too), but I find it the most readable. I have confirmed that the generated assemply is essentially the same, i.e. this doesn't generate _two_ comparison instructions. [no important files changed]
1 parent 5006ebd commit 198d36d

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

exercises/practice/dominoes/.meta/test_template.tera

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn {{ test.description | make_ident }}() {
1515
assert!(dominoes::chain(input).is_none());
1616
{%- endif %}
1717
}
18-
{% endfor -%}
18+
{% endfor %}
1919

2020
type Domino = (u8, u8);
2121

@@ -53,9 +53,6 @@ fn assert_correct(input: &[Domino], output: Vec<Domino>) {
5353
}
5454
}
5555

56-
fn normalize(d: Domino) -> Domino {
57-
match d {
58-
(m, n) if m > n => (n, m),
59-
(m, n) => (m, n),
60-
}
56+
fn normalize((a, b): Domino) -> Domino {
57+
(a.min(b), a.max(b))
6158
}

exercises/practice/dominoes/tests/dominoes.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ fn separate_three_domino_loops() {
111111
let input = &[(1, 2), (2, 3), (3, 1), (4, 5), (5, 6), (6, 4)];
112112
assert!(dominoes::chain(input).is_none());
113113
}
114+
114115
type Domino = (u8, u8);
115116

116117
fn assert_correct(input: &[Domino], output: Vec<Domino>) {
@@ -147,9 +148,6 @@ fn assert_correct(input: &[Domino], output: Vec<Domino>) {
147148
}
148149
}
149150

150-
fn normalize(d: Domino) -> Domino {
151-
match d {
152-
(m, n) if m > n => (n, m),
153-
(m, n) => (m, n),
154-
}
151+
fn normalize((a, b): Domino) -> Domino {
152+
(a.min(b), a.max(b))
155153
}

0 commit comments

Comments
 (0)